~kroq-gar78/ubuntu/precise/tomboy/fix-965786

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
using System;
using System.Collections.Generic;
using Mono.Unix;
using System.Runtime.InteropServices;
#if !WIN32 && !MAC
using GtkBeans;
#endif

namespace Tomboy
{
	public class NoteMenuItem : Gtk.ImageMenuItem
	{
		Note note;
		Gtk.Image pin_img;
		bool pinned;
		bool inhibit_activate;

		static Gdk.Pixbuf note_icon;
		static Gdk.Pixbuf pinup;
		static Gdk.Pixbuf pinup_active;
		static Gdk.Pixbuf pindown;

		static NoteMenuItem ()
		{
			// Cache this since we use it a lot.
			note_icon = GuiUtils.GetIcon ("note", 16);
			pinup = GuiUtils.GetIcon ("pin-up", 16);
			pinup_active = GuiUtils.GetIcon ("pin-active", 16);
			pindown = GuiUtils.GetIcon ("pin-down", 16);
		}

		public NoteMenuItem (Note note, bool show_pin)
			: base (GetDisplayName(note))
		{
			this.note = note;
			Image = new Gtk.Image (note_icon);
#if HAS_GTK_2_16
			this.SetAlwaysShowImage (true);
#endif
			if (show_pin) {
				Gtk.HBox box = new Gtk.HBox (false, 0);
				Gtk.Widget child = Child;
				Remove (child);
				box.PackStart (child, true, true, 0);
				Add (box);
				box.Show();

				pinned = note.IsPinned;
				pin_img = new Gtk.Image(pinned ? pindown : pinup);
				pin_img.Show();
				box.PackStart (pin_img, false, false, 0);
			}
		}

		static string FormatForLabel (string name)
		{
			// Replace underscores ("_") with double-underscores ("__")
			// so Note menuitems are not created with mnemonics.
			return name.Replace ("_", "__");
		}

		static string GetDisplayName (Note note)
		{
			string display_name = note.Title;
			int max_length = (int) Preferences.Get (Preferences.MENU_ITEM_MAX_LENGTH);

			if (note.IsNew) {
				string new_string = Catalog.GetString (" (new)");
				max_length -= new_string.Length;
				display_name = Ellipsify (display_name, max_length)
					+ new_string;
			} else {
				display_name = Ellipsify (display_name, max_length);
			}

			return FormatForLabel (display_name);
		}

		static string Ellipsify (string str, int max)
		{
			if(str.Length > max)
				return str.Substring(0, max - 1) + "...";
			return str;
		}

		protected override void OnActivated ()
		{
			if (!inhibit_activate) {
				if (note != null)
					note.Window.Present ();
			}
		}

		protected override bool OnButtonPressEvent (Gdk.EventButton ev)
		{
			if (pin_img != null &&
			                ev.X >= pin_img.Allocation.X &&
			                ev.X < pin_img.Allocation.X + pin_img.Allocation.Width) {
				pinned = note.IsPinned = !pinned;
				pin_img.Pixbuf = pinned ? pindown : pinup;
				inhibit_activate = true;
				return true;
			}
			return base.OnButtonPressEvent (ev);
		}

		protected override bool OnButtonReleaseEvent (Gdk.EventButton ev)
		{
			if (inhibit_activate) {
				inhibit_activate = false;
				return true;
			}
			return base.OnButtonReleaseEvent (ev);
		}

		protected override bool OnMotionNotifyEvent (Gdk.EventMotion ev)
		{
			if (!pinned && pin_img != null) {
				if (ev.X >= pin_img.Allocation.X &&
				                ev.X < pin_img.Allocation.X + pin_img.Allocation.Width) {
					if (pin_img.Pixbuf != pinup_active)
						pin_img.Pixbuf = pinup_active;
				} else if (pin_img.Pixbuf != pinup) {
					pin_img.Pixbuf = pinup;
				}
			}
			return base.OnMotionNotifyEvent (ev);
		}

		protected override bool OnLeaveNotifyEvent (Gdk.EventCrossing ev)
		{
			if (!pinned && pin_img != null) {
				pin_img.Pixbuf = pinup;
			}
			return base.OnLeaveNotifyEvent (ev);
		}
	}
	
	
	public class TomboyTrayIcon : Gtk.StatusIcon, ITomboyTray
	{
		TomboyTray tray;
		TomboyPrefsKeybinder keybinder;
		Gtk.Menu context_menu;
		Gtk.ImageMenuItem sync_menu_item;

		public TomboyTrayIcon (NoteManager manager)
		{
			tray = new TomboyTray (manager, this);
			keybinder = new TomboyPrefsKeybinder (manager, this);
			int panel_size = 22;
			// Load Icon to display in the notification area.
			// First we try the "tomboy-panel" icon. This icon can be replaced
			// by the user's icon theme. If the theme does not have this icon
			// then we fall back to the Tomboy Menu icon named "tomboy".
			Pixbuf = GuiUtils.GetIcon ("tomboy-panel", panel_size) ??
				GuiUtils.GetIcon ("tomboy", panel_size);

			Tooltip = TomboyTrayUtils.GetToolTipText ();

			Visible = (bool) Preferences.Get (Preferences.ENABLE_TRAY_ICON);
			Preferences.SettingChanged += (o, args) => {
				if (args.Key == Preferences.ENABLE_TRAY_ICON)
					Visible = (bool) args.Value;
			};

			Tomboy.ExitingEvent += OnExit;
#if MAC
			Visible = false;
#endif
		}
		
		public TomboyTray Tray
		{
			get {
				return tray;
			}
		}
		
		protected override void OnActivate()
		{
			ShowMenu (false);
		}
		
		protected override void OnPopupMenu (uint button, uint activate_time)
		{
			if (button == 3)
				GuiUtils.PopupMenu (GetRightClickMenu (),
				                    null, 
				                    new Gtk.MenuPositionFunc (GetTrayMenuPosition));
				
		}
		
		public void ShowMenu (bool select_first_item)
		{
			if (context_menu != null)
				context_menu.Hide ();

			tray.NoteManager.GtkInvoke (() => {
				TomboyTrayUtils.UpdateTomboyTrayMenu (tray, null);
				if (select_first_item)
					tray.TomboyTrayMenu.SelectFirst (false);

				GuiUtils.PopupMenu (tray.TomboyTrayMenu, null,
					new Gtk.MenuPositionFunc (GetTrayMenuPosition));
			});
		}
		
		public void GetTrayMenuPosition (Gtk.Menu menu,
		                             out int  x,
		                             out int  y,
		                             out bool push_in)
		{
			// some default values in case something goes wrong
			push_in = true;
			x = 0;
			y = 0;
			
			Gdk.Screen screen;
			Gdk.Rectangle area;
			try {
#if WIN32 || MAC
				menu.Screen.Display.GetPointer (out x, out y);
				screen = menu.Screen;
				area.Height = 0;
#else
				Gtk.Orientation orientation;
				GetGeometry (out screen, out area, out orientation);
				x = area.X;
				y = area.Y;
#endif

				Gtk.Requisition menu_req = menu.SizeRequest ();
				if (y + menu_req.Height >= screen.Height)
					y -= menu_req.Height;
				else
					y += area.Height;
			} catch (Exception e) {
				Logger.Error ("Exception in GetTrayMenuPosition: " + e.ToString ());
			}
		}

		void Preferences_SettingChanged (object sender, EventArgs args)
		{
			// Update items based on configuration.
			UpdateMenuItems ();
		}

		void UpdateMenuItems ()
		{
			// Is synchronization configured and active?
			string sync_addin_id = Preferences.Get (Preferences.SYNC_SELECTED_SERVICE_ADDIN)
				as string;
			sync_menu_item.Sensitive = !string.IsNullOrEmpty (sync_addin_id);
		}

		Gtk.Menu GetRightClickMenu ()
		{
			if (tray.TomboyTrayMenu != null)
				tray.TomboyTrayMenu.Hide ();

			if (context_menu != null) {
				context_menu.Hide ();
				return context_menu;
			}

			context_menu = new Gtk.Menu ();

			Gtk.AccelGroup accel_group = new Gtk.AccelGroup ();
			context_menu.AccelGroup = accel_group;

			Gtk.ImageMenuItem item;

			sync_menu_item = new Gtk.ImageMenuItem (Catalog.GetString ("S_ynchronize Notes"));
			sync_menu_item.Image = new Gtk.Image (Gtk.Stock.Convert, Gtk.IconSize.Menu);
			UpdateMenuItems();
			Preferences.SettingChanged += Preferences_SettingChanged;
			sync_menu_item.Activated += SyncNotes;
			context_menu.Append (sync_menu_item);

			context_menu.Append (new Gtk.SeparatorMenuItem ());

			item = new Gtk.ImageMenuItem (Catalog.GetString ("_Preferences"));
			item.Image = new Gtk.Image (Gtk.Stock.Preferences, Gtk.IconSize.Menu);
			item.Activated += ShowPreferences;
			context_menu.Append (item);

			item = new Gtk.ImageMenuItem (Catalog.GetString ("_Help"));
			item.Image = new Gtk.Image (Gtk.Stock.Help, Gtk.IconSize.Menu);
			item.Activated += ShowHelpContents;
			context_menu.Append (item);

			item = new Gtk.ImageMenuItem (Catalog.GetString ("_About Tomboy"));
			item.Image = new Gtk.Image (Gtk.Stock.About, Gtk.IconSize.Menu);
			item.Activated += ShowAbout;
			context_menu.Append (item);

			context_menu.Append (new Gtk.SeparatorMenuItem ());

			item = new Gtk.ImageMenuItem (Catalog.GetString ("_Quit"));
			item.Image = new Gtk.Image (Gtk.Stock.Quit, Gtk.IconSize.Menu);
			item.Activated += Quit;
			context_menu.Append (item);

			context_menu.ShowAll ();
			return context_menu;
		}

		void ShowPreferences (object sender, EventArgs args)
		{
			Tomboy.ActionManager ["ShowPreferencesAction"].Activate ();
		}

		void SyncNotes (object sender, EventArgs args)
		{
			Tomboy.ActionManager ["NoteSynchronizationAction"].Activate ();
		}

		void ShowHelpContents (object sender, EventArgs args)
		{
			Tomboy.ActionManager ["ShowHelpAction"].Activate ();
		}

		void ShowAbout (object sender, EventArgs args)
		{
			Tomboy.ActionManager ["ShowAboutAction"].Activate ();
		}

		void Quit (object sender, EventArgs args)
		{
			Tomboy.ActionManager ["QuitTomboyAction"].Activate ();
		}

		void OnExit (object sender, EventArgs e)
		{
			Visible = false;
		}

		public bool MenuOpensUpward ()
		{
			bool open_upwards = false;
			int val = 0;
			Gdk.Screen screen = null;
#if WIN32 || MAC
			int x;
			tray.TomboyTrayMenu.Screen.Display.GetPointer (out x, out val);
			screen = tray.TomboyTrayMenu.Screen;
#else
			Gdk.Rectangle area;
			Gtk.Orientation orientation;
			GetGeometry (out screen, out area, out orientation);
			val = area.Y;
#endif

			Gtk.Requisition menu_req = tray.TomboyTrayMenu.SizeRequest ();
			if (val + menu_req.Height >= screen.Height)
				open_upwards = true;

			return open_upwards;
		}
	}

	// TODO: Some naming love would be nice
	public interface ITomboyTray
	{
		void ShowMenu (bool select_first_item);
		bool MenuOpensUpward ();
	}
	
	public class TomboyTray
	{
		NoteManager manager;
		ITomboyTray tray;
		bool menu_added = false;
		List<Gtk.MenuItem> recent_notes = new List<Gtk.MenuItem> ();
		Gtk.Menu tray_menu;
		
		protected TomboyTray (NoteManager manager)
		{
			this.manager = manager;
			
			tray_menu = MakeTrayNotesMenu ();
		}
		
		public TomboyTray (NoteManager manager, ITomboyTray tray)
			: this (manager)
		{
			this.tray = tray;
		}
		
		Gtk.Menu MakeTrayNotesMenu ()
		{
			Gtk.Menu menu =
			        Tomboy.ActionManager.GetWidget ("/TrayIconMenu") as Gtk.Menu;

			bool enable_keybindings = (bool)
			                          Preferences.Get (Preferences.ENABLE_KEYBINDINGS);
			if (enable_keybindings) {
				// Create New Note Keybinding
				Gtk.MenuItem item =
				        Tomboy.ActionManager.GetWidget (
				                "/TrayIconMenu/TrayNewNotePlaceholder/TrayNewNote") as Gtk.MenuItem;
				if (item != null)
					GConfKeybindingToAccel.AddAccelerator (
					        item,
					        Preferences.KEYBINDING_CREATE_NEW_NOTE);

				// Show Search All Notes Keybinding
				item =
				        Tomboy.ActionManager.GetWidget (
				                "/TrayIconMenu/ShowSearchAllNotes") as Gtk.MenuItem;
				if (item != null)
					GConfKeybindingToAccel.AddAccelerator (
					        item,
					        Preferences.KEYBINDING_OPEN_RECENT_CHANGES);

				// Open Start Here Keybinding
				item =
				        Tomboy.ActionManager.GetWidget (
				                "/TrayIconMenu/OpenStartHereNote") as Gtk.MenuItem;
				if (item != null)
					GConfKeybindingToAccel.AddAccelerator (
					        item,
					        Preferences.KEYBINDING_OPEN_START_HERE);
			}

			return menu;
		}
		
		void RemoveRecentlyChangedNotes ()
		{
			foreach (Gtk.Widget item in recent_notes) {
				tray_menu.Remove (item);
			}

			recent_notes.Clear ();
		}
		
		public void AddRecentlyChangedNotes ()
		{
			int min_size = (int) Preferences.Get (Preferences.MENU_NOTE_COUNT);
			int max_size = 18;
			int list_size = 0;
			bool menuOpensUpward = tray.MenuOpensUpward ();
			NoteMenuItem item;

			// Remove the old dynamic items
			RemoveRecentlyChangedNotes ();

			// Assume menu opens downward, move common items to top of menu
			Gtk.MenuItem newNoteItem = Tomboy.ActionManager.GetWidget (
			                                   "/TrayIconMenu/TrayNewNotePlaceholder/TrayNewNote") as Gtk.MenuItem;
			Gtk.MenuItem searchNotesItem = Tomboy.ActionManager.GetWidget (
			                                       "/TrayIconMenu/ShowSearchAllNotes") as Gtk.MenuItem;
			tray_menu.ReorderChild (newNoteItem, 0);
			int insertion_point = 1; // If menu opens downward
			
			// Find all child widgets under the TrayNewNotePlaceholder
			// element.  Make sure those added by add-ins are
			// properly accounted for and reordered.
			List<Gtk.Widget> newNotePlaceholderWidgets = new List<Gtk.Widget> ();
			IList<Gtk.Widget> allChildWidgets =
				Tomboy.ActionManager.GetPlaceholderChildren ("/TrayIconMenu/TrayNewNotePlaceholder");
			foreach (Gtk.Widget child in allChildWidgets) {
				if (child is Gtk.MenuItem &&
				    child != newNoteItem) {
					newNotePlaceholderWidgets.Add (child);
					tray_menu.ReorderChild (child, insertion_point);
					insertion_point++;
				}
			}
			
			tray_menu.ReorderChild (searchNotesItem, insertion_point);
			insertion_point++;

			DateTime days_ago = DateTime.Today.AddDays (-3);
			
			// Prevent template notes from appearing in the menu
			Tag template_tag = TagManager.GetOrCreateSystemTag (TagManager.TemplateNoteSystemTag);

			// List the most recently changed notes, any currently
			// opened notes, and any pinned notes...
			foreach (Note note in manager.Notes) {
				if (note.IsSpecial)
					continue;
				
				// Skip template notes
				if (note.ContainsTag (template_tag))
					continue;

				bool show = false;

				// Test for note.IsPinned first so that all of the pinned notes
				// are guaranteed to be included regardless of the size of the
				// list.
				if (note.IsPinned) {
					show = true;
				} else if ((note.IsOpened && note.Window.IsMapped) ||
				                note.ChangeDate > days_ago ||
				                list_size < min_size) {
					if (list_size <= max_size)
						show = true;
				}

				if (show) {
					item = new NoteMenuItem (note, true);
					// Add this widget to the menu (+insertion_point to add after new+search+...)
					tray_menu.Insert (item, list_size + insertion_point);
					// Keep track of this item so we can remove it later
					recent_notes.Add (item);

					list_size++;
				}
			}

			Note start = manager.FindByUri (NoteManager.StartNoteUri);
			if (start != null) {
				item = new NoteMenuItem (start, false);
				if (menuOpensUpward)
					tray_menu.Insert (item, list_size + insertion_point);
				else
					tray_menu.Insert (item, insertion_point);
				recent_notes.Add (item);

				list_size++;
				
				bool enable_keybindings = (bool)
					                  Preferences.Get (Preferences.ENABLE_KEYBINDINGS);
				if (enable_keybindings)
					GConfKeybindingToAccel.AddAccelerator (
					        item,
					        Preferences.KEYBINDING_OPEN_START_HERE);
			}


			// FIXME: Rearrange this stuff to have less wasteful reordering
			if (menuOpensUpward) {
				// Relocate common items to bottom of menu
				insertion_point -= 1;
				tray_menu.ReorderChild (searchNotesItem, list_size + insertion_point);
				foreach (Gtk.Widget widget in newNotePlaceholderWidgets)
					tray_menu.ReorderChild (widget, list_size + insertion_point);
				tray_menu.ReorderChild (newNoteItem, list_size + insertion_point);
				insertion_point = list_size;
			}

			Gtk.SeparatorMenuItem separator = new Gtk.SeparatorMenuItem ();
			tray_menu.Insert (separator, insertion_point);
			recent_notes.Add (separator);
		}

		public bool IsMenuAdded
		{
			get { return menu_added; }
			set { menu_added = value; }
		}

		public Gtk.Menu TomboyTrayMenu
		{
			get { return tray_menu; }
		}
		
		public ITomboyTray Tray
		{
			get { return tray; }
		}
		
		public NoteManager NoteManager
		{
			get { return manager; }
		}
	}
	
	public class TomboyTrayUtils
	{
	
		public static string GetToolTipText ()
		{
			string tip_text = Catalog.GetString ("Tomboy Notes");

			if ((bool) Preferences.Get (Preferences.ENABLE_KEYBINDINGS)) {
				string shortcut =
				        GConfKeybindingToAccel.GetShortcut (
				                Preferences.KEYBINDING_SHOW_NOTE_MENU);
				if (shortcut != null)
					tip_text += String.Format (" ({0})", shortcut);
			}
			
			return tip_text;
		}
		
		public static void UpdateTomboyTrayMenu (TomboyTray tray, Gtk.Widget parent)
		{
			if (!tray.IsMenuAdded) {
				if (parent != null)
					tray.TomboyTrayMenu.AttachToWidget (parent, GuiUtils.DetachMenu);
				tray.IsMenuAdded = true;
			}

			tray.AddRecentlyChangedNotes ();

			tray.TomboyTrayMenu.ShowAll ();
		}
	
	}

	//
	// This is a helper to take the XKeybinding string from GConf, and
	// convert it to a widget accelerator label, so note menu items can
	// display their global X keybinding.
	//
	// FIXME: It would be totally sweet to allow setting the accelerator
	// visually through the menuitem, and have the new value be stored in
	// GConf.
	//
	public class GConfKeybindingToAccel
	{
		static Gtk.AccelGroup accel_group;

		static GConfKeybindingToAccel ()
		{
			accel_group = new Gtk.AccelGroup ();
		}

		public static string GetShortcut (string gconf_path)
		{
			try {
				string binding = (string) Preferences.Get (gconf_path);
				if (binding == null ||
				                binding == String.Empty ||
				                binding == "disabled")
					return null;

				binding = binding.Replace ("<", "");
				binding = binding.Replace (">", "-");

				return binding;
			} catch {
				return null;
			}
		}

		public static void AddAccelerator (Gtk.MenuItem item, string gconf_path)
		{
			uint keyval;
			Gdk.ModifierType mods;

			if (Services.Keybinder.GetAccelKeys (gconf_path, out keyval, out mods))
				item.AddAccelerator ("activate",
				                     accel_group,
				                     keyval,
				                     mods,
				                     Gtk.AccelFlags.Visible);
		}
	}
}