Merging from head
[beagle.git] / tools / Settings.cs
blob2cfdfc291e3c88d52ea079e185febcd81a945f98
1 //
2 // Settings.cs
3 //
4 // Copyright (C) 2005 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
27 using System;
28 using System.Collections;
29 using System.IO;
30 using System.Reflection;
31 using System.Threading;
33 using Mono.Unix;
34 using Mono.Unix.Native;
36 using Gtk;
37 using Gdk;
38 using Glade;
40 using Beagle;
41 using Beagle.Util;
43 public class SettingsDialog
45 public static void Main (string[] args)
47 SettingsDialog settings = new SettingsDialog ();
48 settings.Run ();
51 ////////////////////////////////////////////////////////////////
52 // Widgets
54 [Widget] VBox administration_frame;
56 [Widget] CheckButton allow_root_toggle;
57 [Widget] CheckButton autostart_toggle;
58 [Widget] CheckButton battery_toggle;
59 [Widget] CheckButton auto_search_toggle;
60 [Widget] CheckButton press_ctrl_toggle;
61 [Widget] CheckButton press_alt_toggle;
63 [Widget] Entry show_search_window_entry;
65 [Widget] CheckButton index_home_toggle;
66 [Widget] Button remove_include_button;
67 [Widget] Button remove_exclude_button;
69 [Widget] Button display_up_button;
70 [Widget] Button display_down_button;
72 [Widget] Gtk.Window settings_dialog;
74 [Widget] ScrolledWindow include_sw;
75 [Widget] ScrolledWindow exclude_sw;
77 IncludeView include_view;
78 ExcludeView exclude_view;
80 ////////////////////////////////////////////////////////////////
81 // Initialize
83 public SettingsDialog ()
85 Application.Init ();
87 Catalog.Init ("beagle", ExternalStringsHack.LocaleDir);
89 Glade.XML glade = new Glade.XML (null, "settings.glade", "settings_dialog", "beagle");
90 glade.Autoconnect (this);
92 settings_dialog.Icon = Beagle.Images.GetPixbuf ("system-search.png");
93 administration_frame.Visible = (Environment.UserName == "root");
95 include_view = new IncludeView ();
96 include_view.Selection.Changed += new EventHandler (OnIncludeSelected);
97 include_view.Show ();
98 include_sw.Child = include_view;
100 exclude_view = new ExcludeView ();
101 exclude_view.Selection.Changed += new EventHandler (OnExcludeSelected);
102 exclude_view.Show ();
103 exclude_sw.Child = exclude_view;
105 LoadConfiguration ();
107 Conf.Subscribe (typeof (Conf.IndexingConfig), new Conf.ConfigUpdateHandler (OnConfigurationChanged));
108 Conf.Subscribe (typeof (Conf.SearchingConfig), new Conf.ConfigUpdateHandler (OnConfigurationChanged));
111 public void Run ()
113 Application.Run ();
116 ////////////////////////////////////////////////////////////////
117 // Configuration
119 private void LoadConfiguration ()
121 allow_root_toggle.Active = Conf.Daemon.AllowRoot;
122 auto_search_toggle.Active = Conf.Searching.BeagleSearchAutoSearch;
123 battery_toggle.Active = Conf.Indexing.IndexOnBattery;
125 autostart_toggle.Active = IsAutostartEnabled ();
127 KeyBinding show_binding = Conf.Searching.ShowSearchWindowBinding;
128 press_ctrl_toggle.Active = show_binding.Ctrl;
129 press_alt_toggle.Active = show_binding.Alt;
130 show_search_window_entry.Text = show_binding.Key;
132 if (Conf.Indexing.IndexHomeDir)
133 index_home_toggle.Active = true;
135 foreach (string include in Conf.Indexing.Roots)
136 include_view.AddPath (include);
138 foreach (ExcludeItem exclude_item in Conf.Indexing.Excludes)
139 exclude_view.AddItem (exclude_item);
142 private void SaveConfiguration ()
144 Conf.Daemon.AllowRoot = allow_root_toggle.Active;
145 Conf.Searching.BeagleSearchAutoSearch = auto_search_toggle.Active;
146 Conf.Indexing.IndexOnBattery = battery_toggle.Active;
148 Conf.Searching.ShowSearchWindowBinding = new KeyBinding (show_search_window_entry.Text,
149 press_ctrl_toggle.Active,
150 press_alt_toggle.Active);
152 Conf.Indexing.IndexHomeDir = index_home_toggle.Active;
154 Conf.Indexing.Roots = include_view.Includes;
155 Conf.Indexing.Excludes = exclude_view.Excludes;
157 Conf.Save (true);
160 private void OnConfigurationChanged (Conf.Section section)
162 HigMessageDialog dialog = new HigMessageDialog (settings_dialog,
163 DialogFlags.Modal,
164 MessageType.Question,
165 ButtonsType.YesNo,
166 Catalog.GetString ("Reload configuration"),
167 Catalog.GetString ("The configuration file has been modified by another application. " +
168 "Do you wish to discard the currently displayed values and reload configuration from disk?"));
170 ResponseType response = (ResponseType) dialog.Run ();
172 if (response == ResponseType.Yes)
173 LoadConfiguration ();
175 dialog.Destroy ();
178 ////////////////////////////////////////////////////////////////
179 // Autostart
181 private string system_autostart_dir = Path.Combine (Path.Combine (ExternalStringsHack.SysConfDir, "xdg"), "autostart");
182 private string local_autostart_dir = Path.Combine (Path.Combine (Environment.GetEnvironmentVariable ("HOME"), ".config"), "autostart");
184 private bool IsAutostartEnabled ()
186 // FIXME: We need to do better than this.
188 string local_beagled = Path.Combine (local_autostart_dir, "beagled-autostart.desktop");
189 string system_beagled = Path.Combine (system_autostart_dir, "beagled-autostart.desktop");
191 if (File.Exists (local_beagled)) {
192 StreamReader reader = new StreamReader (local_beagled);
194 try {
195 string l;
196 while ((l = reader.ReadLine ()) != null) {
197 if (String.Compare (l, "X-GNOME-Autostart-enabled=false", true) == 0)
198 return false;
201 return true;
202 } finally {
203 reader.Close ();
205 } else if (File.Exists (system_beagled)) {
206 StreamReader reader = new StreamReader (system_beagled);
208 try {
209 string l;
210 while ((l = reader.ReadLine ()) != null) {
211 if (String.Compare (l, "X-GNOME-Autostart-enabled=false", true) == 0)
212 return false;
215 return true;
216 } finally {
217 reader.Close ();
219 } else
220 return false;
223 private void SetAutostart (bool enabled)
225 if (! Directory.Exists (local_autostart_dir)) {
226 Directory.CreateDirectory (local_autostart_dir);
227 Syscall.chmod (local_autostart_dir, (FilePermissions) 448); // 448 == 0700
230 string beagled_file = Path.Combine (local_autostart_dir, "beagled-autostart.desktop");
231 string beagle_search_file = Path.Combine (local_autostart_dir, "beagle-search-autostart.desktop");
233 Assembly assembly = Assembly.GetExecutingAssembly ();
235 StreamReader reader = new StreamReader (assembly.GetManifestResourceStream ("beagled-autostart.desktop"));
236 StreamWriter writer = new StreamWriter (beagled_file);
238 string l;
239 while ((l = reader.ReadLine ()) != null)
240 writer.WriteLine (l);
241 reader.Close ();
243 if (! enabled) {
244 // FIXME: gnome-session has a bug in which autostart overrides
245 // break if Hidden=true is set.
246 writer.WriteLine ("# FIXME: Hidden=true has to be commented out for GNOME autostart to be");
247 writer.WriteLine ("# disabled, but KDE requires it to disable autostart.");
248 writer.WriteLine ("#Hidden=true");
249 writer.WriteLine ("X-GNOME-Autostart-enabled=false");
252 writer.Close ();
254 reader = new StreamReader (assembly.GetManifestResourceStream ("beagle-search-autostart.desktop"));
255 writer = new StreamWriter (beagle_search_file);
257 while ((l = reader.ReadLine ()) != null)
258 writer.WriteLine (l);
259 reader.Close ();
261 if (! enabled) {
262 // FIXME: gnome-session has a bug in which autostart overrides
263 // break if Hidden=true is set.
264 writer.WriteLine ("# FIXME: Hidden=true has to be commented out for GNOME autostart to be");
265 writer.WriteLine ("# disabled, but KDE requires it to disable autostart.");
266 writer.WriteLine ("#Hidden=true");
267 writer.WriteLine ("X-GNOME-Autostart-enabled=false");
270 writer.Close ();
273 ////////////////////////////////////////////////////////////////
274 // Eventhandlers
276 private void OnAutostartToggled (object o, EventArgs args)
278 SetAutostart (((Gtk.ToggleButton) o).Active);
281 private void OnDialogResponse (object o, ResponseArgs args)
283 switch (args.ResponseId) {
284 case ResponseType.Help:
285 Gnome.Url.Show ("http://beagle-project.org/Configuring");
286 break;
287 case ResponseType.Ok:
288 SaveConfiguration ();
289 Application.Quit ();
290 break;
291 default:
292 Application.Quit ();
293 break;
297 private void OnDisplaySelected (object o, EventArgs args)
299 display_up_button.Sensitive = true;
300 display_down_button.Sensitive = true;
303 private void OnAddIncludeClicked (object o, EventArgs args)
305 CompatFileChooserDialog fs_dialog = new CompatFileChooserDialog (Catalog.GetString ("Select Path"),
306 settings_dialog,
307 CompatFileChooserDialog.Action.SelectFolder);
308 fs_dialog.SelectMultiple = false;
310 ResponseType fs_response = (ResponseType) fs_dialog.Run ();
311 string new_include = fs_dialog.Filename;
312 fs_dialog.Destroy ();
314 if (fs_response == ResponseType.Ok) {
315 string error_message = "";
316 bool throw_error = false;
317 ArrayList obsolete_includes = new ArrayList ();
319 // Check and see if the current data collides with the new path in any way
320 // FIXME: Do this with System.IO.Path or something
321 foreach (string old_include in include_view.Includes) {
322 if (new_include == old_include) {
323 throw_error = true;
324 error_message = Catalog.GetString ("The selected path is already selected for indexing and wasn't added.");
325 } else if (new_include.StartsWith (old_include)) {
326 throw_error = true;
327 error_message = Catalog.GetString ("The selected path wasn't added. The list contains items that supersedes it and the data is already being indexed.");
328 } else if (old_include.StartsWith (new_include)) {
329 obsolete_includes.Add (old_include);
333 if (throw_error) {
334 HigMessageDialog.RunHigMessageDialog (settings_dialog,
335 DialogFlags.Modal,
336 MessageType.Warning,
337 ButtonsType.Ok,
338 Catalog.GetString ("Path not added"),
339 error_message);
340 } else {
341 // Confirm the removal of obsolete includes
342 if (obsolete_includes.Count != 0) {
343 HigMessageDialog dialog = new HigMessageDialog (settings_dialog,
344 DialogFlags.Modal,
345 MessageType.Question,
346 ButtonsType.YesNo,
347 Catalog.GetString ("Remove obsolete paths"),
348 Catalog.GetString ("Adding this path will obsolete some of the existing include paths. " +
349 "This will result in the removal of the old obsolete paths. Do you still wish to add it?"));
351 ResponseType confirm_response = (ResponseType) dialog.Run ();
353 if (confirm_response != ResponseType.Yes)
354 return;
356 foreach (string obsolete_include in obsolete_includes)
357 include_view.RemovePath (obsolete_include);
359 dialog.Destroy ();
362 include_view.AddPath (new_include);
367 private void OnRemoveIncludeClicked (object o, EventArgs args)
369 // Confirm removal
370 HigMessageDialog dialog = new HigMessageDialog (settings_dialog,
371 DialogFlags.Modal,
372 MessageType.Question,
373 ButtonsType.YesNo,
374 Catalog.GetString ("Remove path"),
375 Catalog.GetString ("Are you sure you wish to remove this path from the list of directories to be included for indexing?"));
376 ResponseType response = (ResponseType) dialog.Run ();
377 dialog.Destroy ();
379 if (response != ResponseType.Yes)
380 return;
382 include_view.RemoveSelectedPath ();
383 remove_include_button.Sensitive = false;
386 private void OnIncludeSelected (object o, EventArgs args)
388 remove_include_button.Sensitive = true;
391 private void OnAddExcludeClicked (object o, EventArgs args)
393 AddExcludeDialog dialog = new AddExcludeDialog (settings_dialog);
394 dialog.ExcludeItemAddedEvent += new ExcludeItemAddedHandler (OnExcludeItemAdded);
398 private void OnRemoveExcludeClicked (object o, EventArgs args)
400 HigMessageDialog dialog = new HigMessageDialog (settings_dialog,
401 DialogFlags.Modal,
402 MessageType.Question,
403 ButtonsType.YesNo,
404 Catalog.GetString ("Remove item"),
405 Catalog.GetString ("Are you sure you wish to remove this item from the list of data to be excluded from indexing?"));
407 ResponseType response = (ResponseType) dialog.Run ();
408 dialog.Destroy ();
410 if (response != ResponseType.Yes)
411 return;
413 exclude_view.RemoveSelectedItem ();
414 remove_exclude_button.Sensitive = false;
417 private void OnExcludeSelected (object o, EventArgs args)
419 remove_exclude_button.Sensitive = true;
422 private void OnExcludeItemAdded (ExcludeItem exclude_item)
424 exclude_view.AddItem (exclude_item);
427 ////////////////////////////////////////////////////////////////
428 // IncludeView
430 class IncludeView : TreeView
432 private ListStore store;
434 private ArrayList includes = new ArrayList ();
436 public ArrayList Includes {
437 get { return includes; }
440 private enum TargetType {
441 Uri,
444 private static TargetEntry [] target_table = new TargetEntry [] {
445 new TargetEntry ("STRING", 0, (uint) TargetType.Uri ),
446 new TargetEntry ("text/plain", 0, (uint) TargetType.Uri),
449 public IncludeView ()
451 store = new ListStore (typeof (string));
453 this.Model = store;
455 AppendColumn (Catalog.GetString ("Name"), new CellRendererText (), "text", 0);
457 // Enable drag and drop folders from nautilus
458 Gtk.Drag.DestSet (this, DestDefaults.All, target_table, DragAction.Copy | DragAction.Move);
459 DragDataReceived += new DragDataReceivedHandler (HandleData);
462 public void AddPath (string path)
464 includes.Add (path);
465 store.AppendValues (path);
468 public void RemovePath (string path)
470 find_path = path;
471 found_iter = TreeIter.Zero;
473 this.Model.Foreach (new TreeModelForeachFunc (ForeachFindPath));
475 store.Remove (ref found_iter);
476 includes.Remove (path);
479 private string find_path;
480 private TreeIter found_iter;
482 private bool ForeachFindPath (TreeModel model, TreePath path, TreeIter iter)
484 if ((string) model.GetValue (iter, 0) == find_path) {
485 found_iter = iter;
486 return true;
489 return false;
492 public void RemoveSelectedPath ()
494 TreeModel model;
495 TreeIter iter;
497 if (!this.Selection.GetSelected(out model, out iter)) {
498 return;
500 string path = (string)model.GetValue(iter, 0);
502 store.Remove (ref iter);
503 includes.Remove (path);
506 // Handle drag and drop data. Enables users to drag a folder that he wishes
507 // to add for indexing from Nautilus.
508 // FIXME: Pass checks as in OnAddIncludeButtonClicked
509 private void HandleData (object o, DragDataReceivedArgs args) {
510 Uri uri;
511 if (args.SelectionData.Length >=0 && args.SelectionData.Format == 8) {
512 uri = new Uri (args.SelectionData.Text.Trim ());
513 AddPath (uri.LocalPath);
514 Gtk.Drag.Finish (args.Context, true, false, args.Time);
516 Gtk.Drag.Finish (args.Context, false, false, args.Time);
520 ////////////////////////////////////////////////////////////////
521 // Exclude view
523 class ExcludeView : TreeView
525 ArrayList excludes = new ArrayList ();
527 public ArrayList Excludes {
528 get { return excludes; }
531 public ExcludeView ()
533 this.Model = new ListStore (typeof (ExcludeItem));
535 CellRendererText renderer_text = new CellRendererText ();
537 TreeViewColumn type_column = new TreeViewColumn ();
538 type_column.Title = Catalog.GetString ("Type");
539 type_column.PackStart (renderer_text, false);
540 type_column.SetCellDataFunc (renderer_text, new TreeCellDataFunc (TypeCellDataFunc));
541 AppendColumn (type_column);
543 TreeViewColumn name_column = new TreeViewColumn ();
544 name_column.Title = Catalog.GetString ("Name");
545 name_column.PackStart (renderer_text, false);
546 name_column.SetCellDataFunc (renderer_text, new TreeCellDataFunc (NameCellDataFunc));
547 AppendColumn (name_column);
550 public void RemoveSelectedItem ()
552 TreeModel model;
553 TreeIter iter;
555 if (!this.Selection.GetSelected(out model, out iter)) {
556 return;
558 ExcludeItem exclude_item = (ExcludeItem) model.GetValue(iter, 0);
560 ((ListStore)this.Model).Remove (ref iter);
561 excludes.Remove (exclude_item);
564 public void AddItem (ExcludeItem exclude_item)
566 excludes.Add (exclude_item);
567 ((ListStore)this.Model).AppendValues (exclude_item);
570 private void NameCellDataFunc (TreeViewColumn column,
571 CellRenderer renderer,
572 TreeModel model,
573 TreeIter iter)
575 ExcludeItem exclude_item = (ExcludeItem) model.GetValue (iter, 0);
576 if (exclude_item.Type == ExcludeType.MailFolder)
577 ((CellRendererText)renderer).Text = MailFolder.GetNameForPath (exclude_item.Value);
578 else
579 ((CellRendererText)renderer).Text = exclude_item.Value;
582 private void TypeCellDataFunc (TreeViewColumn column,
583 CellRenderer renderer,
584 TreeModel model,
585 TreeIter iter)
587 ExcludeItem exclude_item = (ExcludeItem) model.GetValue (iter, 0);
589 switch (exclude_item.Type) {
590 case ExcludeType.Path:
591 ((CellRendererText)renderer).Text = Catalog.GetString ("Path:");
592 break;
593 case ExcludeType.Pattern:
594 ((CellRendererText)renderer).Text = Catalog.GetString ("Pattern:");
595 break;
596 case ExcludeType.MailFolder:
597 ((CellRendererText)renderer).Text = Catalog.GetString ("Mail folder:");
598 break;
603 ////////////////////////////////////////////////////////////////
604 // PublicfolderView
606 class PublicfolderView : TreeView
608 private ListStore store;
610 private ArrayList publicFolders = new ArrayList ();
612 public ArrayList Publicfolders {
613 get { return publicFolders; }
616 private enum TargetType {
617 Uri,
620 private static TargetEntry [] target_table = new TargetEntry [] {
621 new TargetEntry ("STRING", 0, (uint) TargetType.Uri ),
622 new TargetEntry ("text/plain", 0, (uint) TargetType.Uri),
625 public PublicfolderView ()
627 store = new ListStore (typeof (string));
629 this.Model = store;
631 AppendColumn (Catalog.GetString ("Name"), new CellRendererText (), "text", 0);
633 // Enable drag and drop folders from nautilus
634 Gtk.Drag.DestSet (this, DestDefaults.All, target_table, DragAction.Copy | DragAction.Move);
635 DragDataReceived += new DragDataReceivedHandler (HandleData);
638 public void AddPath (string path)
640 publicFolders.Add (path);
641 store.AppendValues (path);
644 public void RemovePath (string path)
646 find_path = path;
647 found_iter = TreeIter.Zero;
649 this.Model.Foreach (new TreeModelForeachFunc (ForeachFindPath));
651 store.Remove (ref found_iter);
652 publicFolders.Remove (path);
655 private string find_path;
656 private TreeIter found_iter;
658 private bool ForeachFindPath (TreeModel model, TreePath path, TreeIter iter)
660 if ((string) model.GetValue (iter, 0) == find_path) {
661 found_iter = iter;
662 return true;
665 return false;
668 public void RemoveSelectedPath ()
670 TreeModel model;
671 TreeIter iter;
673 if (!this.Selection.GetSelected(out model, out iter)) {
674 return;
676 string path = (string)model.GetValue(iter, 0);
678 store.Remove (ref iter);
679 publicFolders.Remove (path);
682 // Handle drag and drop data. Enables users to drag a folder that he wishes
683 // to add for indexing from Nautilus.
684 // FIXME: Pass checks as in OnAddIncludeButtonClicked
685 private void HandleData (object o, DragDataReceivedArgs args) {
686 Uri uri;
687 if (args.SelectionData.Length >=0 && args.SelectionData.Format == 8) {
688 uri = new Uri (args.SelectionData.Text.Trim ());
689 AddPath (uri.LocalPath);
690 Gtk.Drag.Finish (args.Context, true, false, args.Time);
692 Gtk.Drag.Finish (args.Context, false, false, args.Time);
696 ////////////////////////////////////////////////////////////////
697 // Mail folder dialog
699 class MailFolderDialog
701 Gtk.Window parent;
702 FolderView folder_view;
704 [Widget] Dialog mail_folder_dialog;
705 [Widget] ScrolledWindow folder_sw;
707 public event ExcludeItemAddedHandler ExcludeItemAddedEvent;
709 public MailFolderDialog (Gtk.Window parent)
711 this.parent = parent;
713 Glade.XML glade = new Glade.XML (null, "settings.glade", "mail_folder_dialog", "beagle");
714 glade.Autoconnect (this);
716 folder_view = new FolderView ();
717 folder_view.Show ();
718 folder_sw.Child = folder_view;
721 private void OnDialogResponse (object o, ResponseArgs args)
723 if (args.ResponseId == ResponseType.Cancel) {
724 mail_folder_dialog.Destroy ();
725 return;
728 ExcludeItem exclude_item;
729 object obj = folder_view.GetCurrentItem ();
731 if (obj is MailAccount) {
733 } else if (obj is MailFolder) {
734 MailFolder folder = (MailFolder) obj;
735 exclude_item = new ExcludeItem (ExcludeType.MailFolder, folder.Path);
737 if (ExcludeItemAddedEvent != null)
738 ExcludeItemAddedEvent (exclude_item);
740 mail_folder_dialog.Destroy ();
744 class FolderView : TreeView
746 TreeStore store;
747 Gdk.Pixbuf folder_icon;
749 public FolderView ()
751 store = new TreeStore (typeof (MailFolder));
752 this.Model = store;
754 folder_icon = this.RenderIcon (Stock.Open, IconSize.Menu, "");
756 HeadersVisible = false;
758 TreeViewColumn column = new TreeViewColumn ();
759 CellRendererPixbuf renderer_icon = new CellRendererPixbuf ();
760 column.PackStart (renderer_icon, false);
761 column.SetCellDataFunc (renderer_icon, new TreeCellDataFunc (IconCellDataFunc));
763 CellRendererText renderer_text = new CellRendererText ();
764 column.PackStart (renderer_text, false);
765 column.SetCellDataFunc (renderer_text, new TreeCellDataFunc (NameCellDataFunc));
766 AppendColumn (column);
768 foreach (MailAccount account in Beagle.Util.Evolution.Accounts) {
769 TreeIter iter = store.AppendValues (account);
771 foreach (MailFolder folder in account.Children) {
772 Add (iter, folder);
777 private void Add (TreeIter parent, MailFolder folder)
779 TreeIter current = store.AppendValues (parent, folder);
781 foreach (MailFolder child in folder.Children)
782 Add (current, child);
785 private void IconCellDataFunc (TreeViewColumn column,
786 CellRenderer renderer,
787 TreeModel model,
788 TreeIter iter)
790 object obj = model.GetValue (iter, 0);
791 ((CellRendererPixbuf)renderer).Pixbuf = (obj is MailAccount) ? null : folder_icon;
794 private void NameCellDataFunc (TreeViewColumn column,
795 CellRenderer renderer,
796 TreeModel model,
797 TreeIter iter)
799 object obj = model.GetValue (iter, 0);
801 if (obj is MailAccount) {
802 MailAccount account = obj as MailAccount;
803 ((CellRendererText)renderer).Markup = String.Format ("<b>{0}</b>",account.Name);
804 } else {
805 MailFolder folder = obj as MailFolder;
806 ((CellRendererText)renderer).Text = folder.Name;
810 public MailFolder GetCurrentItem ()
812 TreeModel model;
813 TreeIter iter;
815 if (!this.Selection.GetSelected (out model, out iter)) {
816 return null;
819 return (MailFolder) model.GetValue (iter, 0);
824 ////////////////////////////////////////////////////////////////
825 // Exclude dialog
827 private delegate void ExcludeItemAddedHandler (ExcludeItem item);
829 class AddExcludeDialog
831 Gtk.Window parent;
833 [Widget] Dialog add_exclude_dialog;
834 [Widget] Entry value_entry;
835 [Widget] Label value_name_label;
836 [Widget] Button browse_button;
838 [Widget] RadioButton type_path_radio;
839 [Widget] RadioButton type_pattern_radio;
840 [Widget] RadioButton type_mailfolder_radio;
842 public event ExcludeItemAddedHandler ExcludeItemAddedEvent;
844 private string value;
846 public string Value {
847 get {
848 if (Type == ExcludeType.MailFolder)
849 return value;
850 else
851 return value_entry.Text;
855 public ExcludeType Type {
856 get {
857 if (type_path_radio.Active)
858 return ExcludeType.Path;
859 else if (type_pattern_radio.Active)
860 return ExcludeType.Pattern;
861 else
862 return ExcludeType.MailFolder;
866 public AddExcludeDialog (Gtk.Window parent)
868 this.parent = parent;
870 Glade.XML glade = new Glade.XML (null, "settings.glade", "add_exclude_dialog", "beagle");
871 glade.Autoconnect (this);
874 private void OnBrowseButtonClicked (object o, EventArgs args)
876 switch (Type) {
877 case ExcludeType.Path:
878 CompatFileChooserDialog fs_dialog = new CompatFileChooserDialog (Catalog.GetString ("Select Folder"),
879 add_exclude_dialog,
880 CompatFileChooserDialog.Action.SelectFolder);
881 fs_dialog.SelectMultiple = false;
883 ResponseType response = (ResponseType) fs_dialog.Run ();
885 if (response == ResponseType.Ok)
886 value_entry.Text = fs_dialog.Filename;
888 fs_dialog.Destroy ();
889 break;
890 case ExcludeType.MailFolder:
891 MailFolderDialog mf_dialog = new MailFolderDialog (add_exclude_dialog);
892 mf_dialog.ExcludeItemAddedEvent += new ExcludeItemAddedHandler (OnExcludeItemAdded);
893 break;
897 private void OnRadioGroupChanged (object o, EventArgs args)
899 value_entry.Text = "";
901 switch (Type) {
902 case ExcludeType.Path:
903 browse_button.Sensitive = true;
904 value_name_label.TextWithMnemonic = Catalog.GetString ("P_ath:");
905 value_entry.IsEditable = true;
906 break;
907 case ExcludeType.MailFolder:
908 browse_button.Sensitive = true;
909 value_name_label.TextWithMnemonic = Catalog.GetString ("M_ail folder:");
910 value_entry.IsEditable = false;
911 break;
912 case ExcludeType.Pattern:
913 browse_button.Sensitive = false;
914 value_name_label.TextWithMnemonic = Catalog.GetString ("P_attern:");
915 value_entry.IsEditable = true;
916 break;
920 private void OnExcludeItemAdded (ExcludeItem item)
922 value = item.Value;
923 value_entry.Text = MailFolder.GetNameForPath (item.Value);
926 private void OnDialogResponse (object o, ResponseArgs args)
928 if (((ResponseType)args.ResponseId) == ResponseType.Ok) {
929 ExcludeItem exclude_item = new ExcludeItem (Type, Value);
931 switch (Type) {
932 case ExcludeType.Path:
933 if (!Directory.Exists (Value)) {
934 HigMessageDialog.RunHigMessageDialog(add_exclude_dialog,
935 DialogFlags.Modal,
936 MessageType.Error,
937 ButtonsType.Ok,
938 Catalog.GetString ("Error adding path"),
939 Catalog.GetString ("The specified path could not be found and therefore it could not be added to the list of resources excluded for indexing."));
940 return;
942 break;
945 if (ExcludeItemAddedEvent != null)
946 ExcludeItemAddedEvent (exclude_item);
948 add_exclude_dialog.Destroy ();
951 public void Destroy ()
953 add_exclude_dialog.Destroy ();
957 #if false
958 ////////////////////////////////////////////////////////////////
959 // Display view
961 class DisplayView : TreeView
963 public DisplayView ()
965 ListStore store = new ListStore (typeof(bool),typeof(string));
967 this.Model = store;
969 AppendColumn (Catalog.GetString ("Show"), new CellRendererToggle (), "active", 0);
970 AppendColumn (Catalog.GetString ("Name"), new CellRendererText (), "text", 1);
973 #endif