Commit 96212dd0 authored by Alberts Muktupāvels's avatar Alberts Muktupāvels

sticky-notes: remove global sticky notes instance

parent d4cd17e3
......@@ -30,27 +30,21 @@
G_DEFINE_TYPE (StickyNotesApplet, sticky_notes_applet, GP_TYPE_APPLET)
StickyNotes *stickynotes = NULL;
static void sticky_notes_init (GpApplet *applet);
static void sticky_notes_init (StickyNotesApplet *self);
static void sticky_notes_applet_new (StickyNotesApplet *self);
static void
popup_add_note (StickyNotesApplet *applet)
{
stickynotes_add (gtk_widget_get_screen (GTK_WIDGET (applet)));
}
static void
stickynote_show_notes (gboolean visible)
stickynote_show_notes (StickyNotesApplet *self,
gboolean visible)
{
StickyNote *note;
GList *l;
if (stickynotes->visible == visible) return;
stickynotes->visible = visible;
if (self->visible == visible)
return;
self->visible = visible;
for (l = stickynotes->notes; l; l = l->next)
for (l = self->notes; l; l = l->next)
{
note = l->data;
stickynote_set_visible (note, visible);
......@@ -58,9 +52,9 @@ stickynote_show_notes (gboolean visible)
}
static void
stickynote_toggle_notes_visible (void)
stickynote_toggle_notes_visible (StickyNotesApplet *self)
{
stickynote_show_notes(!stickynotes->visible);
stickynote_show_notes (self, !self->visible);
}
static void
......@@ -68,8 +62,11 @@ menu_new_note_cb (GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
StickyNotesApplet *applet = (StickyNotesApplet *) user_data;
popup_add_note (applet);
StickyNotesApplet *self;
self = STICKY_NOTES_APPLET (user_data);
stickynotes_add (self);
}
static void
......@@ -77,7 +74,11 @@ menu_hide_notes_cb (GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
stickynote_show_notes (FALSE);
StickyNotesApplet *self;
self = STICKY_NOTES_APPLET (user_data);
stickynote_show_notes (self, FALSE);
}
static void
......@@ -95,10 +96,14 @@ menu_toggle_lock_state (GSimpleAction *action,
GVariant *value,
gpointer user_data)
{
gboolean locked = g_variant_get_boolean (value);
StickyNotesApplet *self;
gboolean locked;
if (g_settings_is_writable (stickynotes->settings, KEY_LOCKED))
g_settings_set_boolean (stickynotes->settings, KEY_LOCKED, locked);
self = STICKY_NOTES_APPLET (user_data);
locked = g_variant_get_boolean (value);
if (g_settings_is_writable (self->settings, KEY_LOCKED))
g_settings_set_boolean (self->settings, KEY_LOCKED, locked);
}
static void
......@@ -107,15 +112,15 @@ destroy_all_response_cb (GtkDialog *dialog,
StickyNotesApplet *applet)
{
if (id == GTK_RESPONSE_OK) {
while (g_list_length(stickynotes->notes) > 0) {
StickyNote *note = g_list_nth_data(stickynotes->notes, 0);
while (g_list_length (applet->notes) > 0) {
StickyNote *note = g_list_nth_data (applet->notes, 0);
stickynote_free(note);
stickynotes->notes = g_list_remove(stickynotes->notes, note);
applet->notes = g_list_remove (applet->notes, note);
}
}
stickynotes_applet_update_tooltips();
stickynotes_save();
stickynotes_applet_update_tooltips (applet);
stickynotes_save (applet);
gtk_widget_destroy (GTK_WIDGET (dialog));
applet->destroy_all_dialog = NULL;
......@@ -182,7 +187,7 @@ menu_preferences_cb (GSimpleAction *action,
return;
}
self->w_prefs = sticky_notes_preferences_new (stickynotes->settings);
self->w_prefs = sticky_notes_preferences_new (self->settings);
g_object_add_weak_pointer (G_OBJECT (self->w_prefs),
(gpointer *) &self->w_prefs);
......@@ -224,16 +229,14 @@ static const GActionEntry stickynotes_applet_menu_actions [] = {
static void
sticky_notes_applet_setup (StickyNotesApplet *self)
{
if (stickynotes == NULL)
sticky_notes_init (GP_APPLET (self));
sticky_notes_init (self);
sticky_notes_applet_new (self);
/* Add applet to linked list of all applets */
stickynotes->applets = g_list_append (stickynotes->applets, self);
stickynotes_load (self);
stickynotes_applet_update_menus ();
stickynotes_applet_update_tooltips ();
stickynotes_applet_update_menus (self);
stickynotes_applet_update_tooltips (self);
}
static void
......@@ -316,9 +319,9 @@ stickynotes_make_prelight_icon (GdkPixbuf *dest, GdkPixbuf *src, int shift)
}
static void
preferences_apply_cb (GSettings *settings,
const gchar *key,
gpointer user_data)
preferences_apply_cb (GSettings *settings,
const gchar *key,
StickyNotesApplet *self)
{
GList *l;
StickyNote *note;
......@@ -326,13 +329,13 @@ preferences_apply_cb (GSettings *settings,
if (!strcmp (key, KEY_STICKY))
{
if (g_settings_get_boolean (settings, key))
for (l = stickynotes->notes; l; l = l->next)
for (l = self->notes; l; l = l->next)
{
note = l->data;
gtk_window_stick (GTK_WINDOW (note->w_window));
}
else
for (l= stickynotes->notes; l; l = l->next)
for (l= self->notes; l; l = l->next)
{
note = l->data;
gtk_window_unstick (GTK_WINDOW (
......@@ -342,19 +345,19 @@ preferences_apply_cb (GSettings *settings,
else if (!strcmp (key, KEY_LOCKED))
{
for (l = stickynotes->notes; l; l = l->next)
for (l = self->notes; l; l = l->next)
{
note = l->data;
stickynote_set_locked (note, g_settings_get_boolean (settings, key));
}
stickynotes_save();
stickynotes_save (self);
}
else if (!strcmp (key, KEY_USE_SYSTEM_COLOR) ||
!strcmp (key, KEY_DEFAULT_FONT_COLOR) ||
!strcmp (key, KEY_DEFAULT_COLOR))
{
for (l = stickynotes->notes; l; l = l->next)
for (l = self->notes; l; l = l->next)
{
note = l->data;
stickynote_set_color (note,
......@@ -366,7 +369,7 @@ preferences_apply_cb (GSettings *settings,
else if (!strcmp (key, KEY_USE_SYSTEM_FONT) ||
!strcmp (key, KEY_DEFAULT_FONT))
{
for (l = stickynotes->notes; l; l = l->next)
for (l = self->notes; l; l = l->next)
{
note = l->data;
stickynote_set_font (note, note->font, FALSE);
......@@ -375,7 +378,7 @@ preferences_apply_cb (GSettings *settings,
else if (!strcmp (key, KEY_FORCE_DEFAULT))
{
for (l = stickynotes->notes; l; l = l->next)
for (l = self->notes; l; l = l->next)
{
note = l->data;
stickynote_set_color(note,
......@@ -385,7 +388,7 @@ preferences_apply_cb (GSettings *settings,
}
}
stickynotes_applet_update_menus();
stickynotes_applet_update_menus (self);
}
static gboolean
......@@ -423,17 +426,18 @@ desktop_window_event_filter (GdkXEvent *xevent,
GdkEvent *event,
gpointer data)
{
gboolean desktop_hide = g_settings_get_boolean (stickynotes->settings, KEY_DESKTOP_HIDE);
StickyNotesApplet *self = data;
gboolean desktop_hide = g_settings_get_boolean (self->settings, KEY_DESKTOP_HIDE);
if (desktop_hide &&
(((XEvent*)xevent)->xany.type == PropertyNotify) &&
(((XEvent*)xevent)->xproperty.atom == gdk_x11_get_xatom_by_name ("_NET_WM_USER_TIME"))) {
stickynote_show_notes (FALSE);
stickynote_show_notes (self, FALSE);
}
return GDK_FILTER_CONTINUE;
}
static void
install_check_click_on_desktop (void)
install_check_click_on_desktop (StickyNotesApplet *self)
{
Window desktop_window;
GdkWindow *window;
......@@ -482,32 +486,23 @@ install_check_click_on_desktop (void)
}
gdk_window_set_events (window, GDK_PROPERTY_CHANGE_MASK);
gdk_window_add_filter (window, desktop_window_event_filter, NULL);
gdk_window_add_filter (window, desktop_window_event_filter, self);
}
/* Create and initalize global sticky notes instance */
static void
sticky_notes_init (GpApplet *applet)
sticky_notes_init (StickyNotesApplet *self)
{
stickynotes = g_new(StickyNotes, 1);
stickynotes->notes = NULL;
stickynotes->applets = NULL;
stickynotes->settings = gp_applet_settings_new (applet, STICKYNOTES_SCHEMA);
stickynotes->last_timeout_data = 0;
stickynotes->visible = TRUE;
self->settings = gp_applet_settings_new (GP_APPLET (self), STICKYNOTES_SCHEMA);
g_signal_connect (stickynotes->settings, "changed",
G_CALLBACK (preferences_apply_cb), NULL);
/* Max height for large notes*/
stickynotes->max_height = 0.8*gdk_screen_get_height( gdk_screen_get_default() );
g_signal_connect (self->settings,
"changed",
G_CALLBACK (preferences_apply_cb),
self);
/* Load sticky notes */
stickynotes_load (gtk_widget_get_screen (GTK_WIDGET (applet)));
self->max_height = 0.8 * gdk_screen_get_height (gdk_screen_get_default ());
self->visible = TRUE;
install_check_click_on_desktop ();
install_check_click_on_desktop (self);
}
static gboolean
......@@ -517,12 +512,12 @@ applet_button_cb (GtkWidget *widget,
{
if (event->type == GDK_2BUTTON_PRESS)
{
popup_add_note (applet);
stickynotes_add (applet);
return TRUE;
}
else if (event->button == 1)
{
stickynote_toggle_notes_visible ();
stickynote_toggle_notes_visible (applet);
return TRUE;
}
......@@ -540,7 +535,7 @@ applet_key_cb (GtkWidget *widget,
case GDK_KEY_space:
case GDK_KEY_KP_Enter:
case GDK_KEY_Return:
stickynote_show_notes (TRUE);
stickynote_show_notes (applet, TRUE);
return TRUE;
default:
......@@ -604,21 +599,16 @@ applet_destroy_cb (GtkWidget *widget,
{
GList *notes;
stickynotes_save_now ();
stickynotes_save_now (applet);
if (applet->destroy_all_dialog != NULL)
gtk_widget_destroy (applet->destroy_all_dialog);
if (stickynotes->applets != NULL)
stickynotes->applets = g_list_remove (stickynotes->applets, applet);
if (stickynotes->applets == NULL) {
notes = stickynotes->notes;
while (notes) {
StickyNote *note = notes->data;
stickynote_free (note);
notes = g_list_next (notes);
}
notes = applet->notes;
while (notes) {
StickyNote *note = notes->data;
stickynote_free (note);
notes = g_list_next (notes);
}
}
......@@ -692,6 +682,7 @@ sticky_notes_applet_new (StickyNotesApplet *applet)
atk_obj = gtk_widget_get_accessible (GTK_WIDGET (applet));
atk_object_set_name (atk_obj, _("Sticky Notes"));
/* Show the applet */
gtk_widget_show_all (GTK_WIDGET (applet));
}
......@@ -726,54 +717,48 @@ void stickynotes_applet_update_icon(StickyNotesApplet *applet)
g_object_unref(pixbuf2);
}
void stickynotes_applet_update_menus(void)
void
stickynotes_applet_update_menus (StickyNotesApplet *self)
{
GList *l;
GAction *action;
gboolean locked_writable;
gboolean locked;
gboolean locked = g_settings_get_boolean (stickynotes->settings, KEY_LOCKED);
gboolean locked_writable = g_settings_is_writable (stickynotes->settings, KEY_LOCKED);
action = gp_applet_menu_lookup_action (GP_APPLET (self), "lock");
locked_writable = g_settings_is_writable (self->settings, KEY_LOCKED);
locked = g_settings_get_boolean (self->settings, KEY_LOCKED);
for (l = stickynotes->applets; l != NULL; l = l->next) {
StickyNotesApplet *applet = l->data;
GAction *action = gp_applet_menu_lookup_action (GP_APPLET (applet), "lock");
g_simple_action_set_enabled (G_SIMPLE_ACTION (action), locked_writable);
g_simple_action_set_state (G_SIMPLE_ACTION (action), g_variant_new_boolean (locked));
}
g_simple_action_set_enabled (G_SIMPLE_ACTION (action), locked_writable);
g_simple_action_set_state (G_SIMPLE_ACTION (action), g_variant_new_boolean (locked));
}
void
stickynotes_applet_update_tooltips (void)
stickynotes_applet_update_tooltips (StickyNotesApplet *self)
{
int num;
char *tooltip, *no_notes;
StickyNotesApplet *applet;
GList *l;
num = g_list_length (stickynotes->notes);
num = g_list_length (self->notes);
no_notes = g_strdup_printf (ngettext ("%d note", "%d notes", num), num);
tooltip = g_strdup_printf ("%s\n%s", _("Show sticky notes"), no_notes);
for (l = stickynotes->applets; l; l = l->next)
{
applet = l->data;
gtk_widget_set_tooltip_text (GTK_WIDGET (applet), tooltip);
}
gtk_widget_set_tooltip_text (GTK_WIDGET (self), tooltip);
g_free (tooltip);
g_free (no_notes);
}
void
stickynotes_applet_panel_icon_get_geometry (int *x, int *y, int *width, int *height)
stickynotes_applet_panel_icon_get_geometry (StickyNotesApplet *applet,
int *x,
int *y,
int *width,
int *height)
{
GtkWidget *widget;
GtkAllocation allocation;
GtkRequisition requisition;
StickyNotesApplet *applet;
applet = stickynotes->applets->data;
widget = GTK_WIDGET (applet->w_image);
......
......@@ -29,25 +29,12 @@
G_DECLARE_FINAL_TYPE (StickyNotesApplet, sticky_notes_applet,
STICKY_NOTES, APPLET, GpApplet)
/* Global Sticky Notes instance */
typedef struct
{
GList *notes; /* Linked-List of all the sticky notes */
GList *applets; /* Linked-List of all the applets */
GSettings *settings;
gint max_height;
guint last_timeout_data;
gboolean visible; /* Toggle show/hide notes */
} StickyNotes;
/* Sticky Notes Applet */
struct _StickyNotesApplet
{
GpApplet parent;
GSettings *settings;
GtkWidget *w_image; /* The applet icon */
GdkPixbuf *icon_normal; /* Normal applet icon */
......@@ -62,15 +49,25 @@ struct _StickyNotesApplet
GtkOrientation panel_orient;
GtkWidget *w_prefs;
};
extern StickyNotes *stickynotes;
gboolean save_scheduled;
GList *notes;
gint max_height;
gboolean visible;
};
void stickynotes_applet_update_icon(StickyNotesApplet *applet);
void stickynotes_applet_update_menus(void);
void stickynotes_applet_update_tooltips(void);
void stickynotes_applet_update_icon (StickyNotesApplet *self);
void stickynotes_applet_update_menus (StickyNotesApplet *self);
void stickynotes_applet_update_tooltips (StickyNotesApplet *self);
void stickynotes_applet_panel_icon_get_geometry (int *x, int *y, int *width, int *height);
void stickynotes_applet_panel_icon_get_geometry (StickyNotesApplet *self,
int *x,
int *y,
int *width,
int *height);
void stickynotes_applet_setup_about (GtkAboutDialog *dialog);
......
......@@ -35,8 +35,6 @@
#define STICKYNOTES_ICON_SIZE 8
static gboolean save_scheduled = FALSE;
static void response_cb (GtkWidget *dialog, gint id, gpointer data);
static void
......@@ -45,7 +43,7 @@ popup_create_cb (GSimpleAction *action,
gpointer user_data)
{
StickyNote *note = (StickyNote *) user_data;
stickynotes_add (gtk_widget_get_screen (note->w_window));
stickynotes_add (note->applet);
}
static void
......@@ -251,7 +249,7 @@ stickynote_configure_cb (GtkWidget *widget,
note->w = event->width;
note->h = event->height;
stickynotes_save();
stickynotes_save (note->applet);
return FALSE;
}
......@@ -332,41 +330,57 @@ properties_activate_cb (GtkWidget *widget,
gtk_dialog_response (GTK_DIALOG (note->w_properties), GTK_RESPONSE_CLOSE);
}
/* Called when a timeout occurs. */
static gboolean
timeout_happened (gpointer data)
timeout_cb (gpointer user_data)
{
if (GPOINTER_TO_UINT (data) == stickynotes->last_timeout_data)
stickynotes_save ();
return FALSE;
StickyNote *note;
note = user_data;
note->buffer_changed_id = 0;
stickynotes_save (note->applet);
return G_SOURCE_REMOVE;
}
/* Called when a text buffer is changed. */
static void
buffer_changed (GtkTextBuffer *buffer, StickyNote *note)
{
if ( (note->h + note->y) > stickynotes->max_height )
gtk_scrolled_window_set_policy ( GTK_SCROLLED_WINDOW(note->w_scroller),
GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
if ((note->h + note->y) > note->applet->max_height)
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (note->w_scroller),
GTK_POLICY_NEVER,
GTK_POLICY_AUTOMATIC);
/* When a buffer is changed, we set a 10 second timer. When
the timer triggers, we will save the buffer if there have
been no subsequent changes. */
++stickynotes->last_timeout_data;
g_timeout_add_seconds (10, (GSourceFunc) timeout_happened,
GUINT_TO_POINTER (stickynotes->last_timeout_data));
if (note->buffer_changed_id != 0)
g_source_remove (note->buffer_changed_id);
note->buffer_changed_id = g_timeout_add_seconds (10, timeout_cb, note);
}
/* Create a new (empty) Sticky Note at a specific position
and with specific size */
static StickyNote *
stickynote_new_aux (GdkScreen *screen, gint x, gint y, gint w, gint h)
stickynote_new_aux (StickyNotesApplet *applet,
gint x,
gint y,
gint w,
gint h)
{
GdkScreen *screen;
StickyNote *note;
GtkBuilder *builder;
static guint id = 0;
screen = gtk_widget_get_screen (GTK_WIDGET (applet));
note = g_new (StickyNote, 1);
note->applet = applet;
note->buffer_changed_id = 0;
builder = gtk_builder_new ();
gtk_builder_add_from_resource (builder,
......@@ -440,13 +454,13 @@ stickynote_new_aux (GdkScreen *screen, gint x, gint y, gint w, gint h)
note->h = h;
/* Customize the window */
if (g_settings_get_boolean (stickynotes->settings, KEY_STICKY))
if (g_settings_get_boolean (applet->settings, KEY_STICKY))
gtk_window_stick(GTK_WINDOW(note->w_window));
if (w == 0 || h == 0)
gtk_window_resize (GTK_WINDOW(note->w_window),
g_settings_get_int (stickynotes->settings, KEY_DEFAULT_WIDTH),
g_settings_get_int (stickynotes->settings, KEY_DEFAULT_HEIGHT));
g_settings_get_int (applet->settings, KEY_DEFAULT_WIDTH),
g_settings_get_int (applet->settings, KEY_DEFAULT_HEIGHT));
else
gtk_window_resize (GTK_WINDOW(note->w_window),
note->w,
......@@ -553,9 +567,9 @@ stickynote_new_aux (GdkScreen *screen, gint x, gint y, gint w, gint h)
/* Create a new (empty) Sticky Note */
StickyNote *
stickynote_new (GdkScreen *screen)
stickynote_new (StickyNotesApplet *applet)
{
return stickynote_new_aux (screen, -1, -1, 0, 0);
return stickynote_new_aux (applet, -1, -1, 0, 0);
}
/* Destroy a Sticky Note */
......@@ -579,9 +593,12 @@ void stickynote_free(StickyNote *note)
/* Change the sticky note title and color */
void stickynote_change_properties (StickyNote *note)
{
StickyNotesApplet *applet;
GdkRGBA color, font_color;
char *color_str = NULL;
applet = note->applet;
gtk_entry_set_text(GTK_ENTRY(note->w_entry),
gtk_label_get_text (GTK_LABEL (note->w_title)));
......@@ -592,7 +609,7 @@ void stickynote_change_properties (StickyNote *note)
color_str = g_strdup (note->color);
else
{
color_str = g_settings_get_string (stickynotes->settings, KEY_DEFAULT_COLOR);
color_str = g_settings_get_string (applet->settings, KEY_DEFAULT_COLOR);
}
if (!IS_STRING_EMPTY (color_str))
......@@ -606,7 +623,7 @@ void stickynote_change_properties (StickyNote *note)
color_str = g_strdup (note->font_color);
else
{
color_str = g_settings_get_string (stickynotes->settings, KEY_DEFAULT_FONT_COLOR);
color_str = g_settings_get_string (applet->settings, KEY_DEFAULT_FONT_COLOR);
}
if (!IS_STRING_EMPTY (color_str))
......@@ -624,7 +641,7 @@ void stickynote_change_properties (StickyNote *note)
gtk_widget_show (note->w_properties);
stickynotes_save();
stickynotes_save (applet);
}
static void
......@@ -651,7 +668,7 @@ void stickynote_set_title(StickyNote *note, const gchar *title)
/* If title is NULL, use the current date as the title. */
if (!title) {
gchar *date_title, *tmp;
gchar *date_format = g_settings_get_string (stickynotes->settings, KEY_DATE_FORMAT);
gchar *date_format = g_settings_get_string (note->applet->settings, KEY_DATE_FORMAT);
if (IS_STRING_EMPTY (date_format)) {
g_free (date_format);
date_format = g_strdup ("%x");
......@@ -919,9 +936,9 @@ update_css (StickyNote *note)
string = g_string_new (NULL);
append_background_color (note, stickynotes->settings, string);
append_font_color (note, stickynotes->settings, string);
append_font (note, stickynotes->settings, string);
append_background_color (note, note->applet->settings, string);
append_font_color (note, note->applet->settings, string);
append_font (note, note->applet->settings, string);
css = g_string_free (string, FALSE);
gtk_css_provider_load_from_data (note->css, css, -1, NULL);
......@@ -997,7 +1014,7 @@ stickynote_set_locked (StickyNote *note,
gtk_image_set_pixel_size (note->img_lock, STICKYNOTES_ICON_SIZE);
stickynotes_applet_update_menus();
stickynotes_applet_update_menus (note->applet);
}
/* Show/Hide a sticky note */
......@@ -1012,7 +1029,7 @@ stickynote_set_visible (StickyNote *note, gboolean visible)
gtk_window_move (GTK_WINDOW (note->w_window),
note->x, note->y);
/* Put the note on all workspaces if necessary. */
if (g_settings_get_boolean (stickynotes->settings, KEY_STICKY))
if (g_settings_get_boolean (note->applet->settings, KEY_STICKY))
gtk_window_stick(GTK_WINDOW(note->w_window));
else if (note->workspace > 0)
{
......@@ -1044,7 +1061,7 @@ stickynote_set_visible (StickyNote *note, gboolean visible)
else {
/* Hide sticky note */
int x, y, width, height;
stickynotes_applet_panel_icon_get_geometry (&x, &y, &width, &height);