Compute lucene-style scores for our hits.
[beagle.git] / tools / Settings.cs
blobe7e299d06324aaecf3fd88844ac6da095d9ffdd4
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.IO;
29 using System.Collections;
30 using System.Threading;
32 using Gtk;
33 using Gdk;
34 using GConf;
35 using Glade;
36 using Mono.Posix;
38 using Beagle;
39 using Beagle.Util;
41 public class SettingsDialog
43 public static void Main (string[] args)
45 SettingsDialog settings = new SettingsDialog ();
46 settings.Run ();
49 ////////////////////////////////////////////////////////////////
50 // Widgets
52 [Widget] CheckButton autostart_toggle;
54 [Widget] CheckButton press_ctrl_toggle;
55 [Widget] CheckButton press_alt_toggle;
56 [Widget] Entry show_search_window_entry;
58 [Widget] CheckButton index_home_toggle;
59 [Widget] Button remove_include_button;
60 [Widget] Button remove_exclude_button;
62 [Widget] Button display_up_button;
63 [Widget] Button display_down_button;
65 [Widget] Gtk.Window settings_dialog;
67 [Widget] ScrolledWindow display_sw;
69 [Widget] ScrolledWindow include_sw;
70 [Widget] ScrolledWindow exclude_sw;
72 #if false
73 DisplayView display_view;
74 #endif
76 IncludeView include_view;
77 ExcludeView exclude_view;
79 ////////////////////////////////////////////////////////////////
80 // WebServices
82 [Widget] VBox networking_box;
83 [Widget] CheckButton allow_global_access_toggle;
84 [Widget] Button remove_publicfolder_button;
85 [Widget] Button remove_netbeagle_button;
87 [Widget] ScrolledWindow publicfolder_sw;
88 [Widget] ScrolledWindow netbeagle_sw;
90 [Widget] Dialog netbeagle_entry_dialog;
91 [Widget] Entry netbeagle_textentry;
93 PublicfolderView publicfolder_view;
94 NetbeagleView netbeagle_view;
96 ////////////////////////////////////////////////////////////////
97 // Initialize
99 public SettingsDialog ()
101 Application.Init ();
103 Catalog.Init ("beagle", ExternalStringsHack.LocaleDir);
105 Glade.XML glade = new Glade.XML (null, "settings.glade", "settings_dialog", "beagle");
106 glade.Autoconnect (this);
108 #if false
109 display_view = new DisplayView ();
110 display_view.Selection.Changed += new EventHandler (OnDisplaySelected);
111 display_view.Show ();
112 // The display sorting stuff is something i have been testing locally, Best CVS wont use it
113 display_sw.Child = display_view;
114 #endif
115 include_view = new IncludeView ();
116 include_view.Selection.Changed += new EventHandler (OnIncludeSelected);
117 include_view.Show ();
118 include_sw.Child = include_view;
120 exclude_view = new ExcludeView ();
121 exclude_view.Selection.Changed += new EventHandler (OnExcludeSelected);
122 exclude_view.Show ();
123 exclude_sw.Child = exclude_view;
125 #if ENABLE_WEBSERVICES
126 networking_box.Visible = true;
128 publicfolder_view = new PublicfolderView ();
129 publicfolder_view.Selection.Changed += new EventHandler (OnPublicfolderSelected);
130 publicfolder_view.Show ();
131 publicfolder_sw.Child = publicfolder_view;
133 netbeagle_view = new NetbeagleView ();
134 netbeagle_view.Selection.Changed += new EventHandler (OnNetbeagleSelected);
135 netbeagle_view.Show ();
136 netbeagle_sw.Child = netbeagle_view;
137 #endif
138 LoadConfiguration ();
140 Conf.Subscribe (typeof (Conf.IndexingConfig), new Conf.ConfigUpdateHandler (OnConfigurationChanged));
141 Conf.Subscribe (typeof (Conf.SearchingConfig), new Conf.ConfigUpdateHandler (OnConfigurationChanged));
143 #if ENABLE_WEBSERVICES
144 Conf.Subscribe (typeof (Conf.NetworkingConfig), new Conf.ConfigUpdateHandler (OnConfigurationChanged));
145 Conf.Subscribe (typeof (Conf.WebServicesConfig), new Conf.ConfigUpdateHandler (OnConfigurationChanged));
146 #endif
149 public void Run ()
151 Application.Run ();
154 ////////////////////////////////////////////////////////////////
155 // Configuration
157 private void LoadConfiguration ()
159 autostart_toggle.Active = Conf.Searching.Autostart;
161 KeyBinding show_binding = Conf.Searching.ShowSearchWindowBinding;
162 press_ctrl_toggle.Active = show_binding.Ctrl;
163 press_alt_toggle.Active = show_binding.Alt;
164 show_search_window_entry.Text = show_binding.Key;
166 if (Conf.Indexing.IndexHomeDir)
167 index_home_toggle.Active = true;
169 foreach (string include in Conf.Indexing.Roots)
170 include_view.AddPath (include);
172 foreach (ExcludeItem exclude_item in Conf.Indexing.Excludes)
173 exclude_view.AddItem (exclude_item);
175 #if ENABLE_WEBSERVICES
176 foreach (string netbeagle in Conf.Networking.NetBeagleNodes)
177 netbeagle_view.AddNode (netbeagle);
179 if (Conf.WebServices.AllowGlobalAccess)
180 allow_global_access_toggle.Active = true;
182 foreach (string publicfolder in Conf.WebServices.PublicFolders)
183 publicfolder_view.AddPath (publicfolder);
184 #endif
187 private void SaveConfiguration ()
189 Conf.Searching.Autostart = autostart_toggle.Active;
191 Conf.Searching.ShowSearchWindowBinding = new KeyBinding (show_search_window_entry.Text,
192 press_ctrl_toggle.Active,
193 press_alt_toggle.Active);
195 Conf.Indexing.IndexHomeDir = index_home_toggle.Active;
197 Conf.Indexing.Roots = include_view.Includes;
198 Conf.Indexing.Excludes = exclude_view.Excludes;
200 #if ENABLE_WEBSERVICES
201 Conf.Networking.NetBeagleNodes = netbeagle_view.Netbeagles;
202 Conf.WebServices.AllowGlobalAccess = allow_global_access_toggle.Active;
203 Conf.WebServices.PublicFolders = publicfolder_view.Publicfolders;
204 #endif
205 Conf.Save (true);
208 private void OnConfigurationChanged (Conf.Section section)
210 HigMessageDialog dialog = new HigMessageDialog (settings_dialog,
211 DialogFlags.Modal,
212 MessageType.Question,
213 ButtonsType.YesNo,
214 Catalog.GetString ("Reload configuration"),
215 Catalog.GetString ("The configuration file has been modified by another application. " +
216 "Do you wish to discard the currently displayed values and reload configuration from disk?"));
218 ResponseType response = (ResponseType) dialog.Run ();
220 if (response == ResponseType.Yes)
221 LoadConfiguration ();
223 dialog.Destroy ();
226 ////////////////////////////////////////////////////////////////
227 // Eventhandlers
229 private void OnDialogResponse (object o, ResponseArgs args)
231 switch (args.ResponseId) {
232 case ResponseType.Help:
233 Gnome.Url.Show ("http://www.beagle-project.org/Configuration_Help");
234 break;
235 case ResponseType.Ok:
236 SaveConfiguration ();
237 Application.Quit ();
238 break;
239 default:
240 Application.Quit ();
241 break;
245 private void OnDisplaySelected (object o, EventArgs args)
247 display_up_button.Sensitive = true;
248 display_down_button.Sensitive = true;
251 private void OnAddIncludeClicked (object o, EventArgs args)
253 CompatFileChooserDialog fs_dialog = new CompatFileChooserDialog (Catalog.GetString ("Select Path"),
254 settings_dialog,
255 CompatFileChooserDialog.Action.SelectFolder);
256 fs_dialog.SelectMultiple = false;
258 ResponseType fs_response = (ResponseType) fs_dialog.Run ();
259 string new_include = fs_dialog.Filename;
260 fs_dialog.Destroy ();
262 if (fs_response == ResponseType.Ok) {
263 string error_message = "";
264 bool throw_error = false;
265 ArrayList obsolete_includes = new ArrayList ();
267 // Check and see if the current data collides with the new path in any way
268 // FIXME: Do this with System.IO.Path or something
269 foreach (string old_include in include_view.Includes) {
270 if (new_include == old_include) {
271 throw_error = true;
272 error_message = Catalog.GetString ("The selected path is already selected for indexing and wasn't added.");
273 } else if (new_include.StartsWith (old_include)) {
274 throw_error = true;
275 error_message = Catalog.GetString ("The selected path wasn't added. The list contains items that supercedes it and the data is already being indexed.");
276 } else if (old_include.StartsWith (new_include)) {
277 obsolete_includes.Add (old_include);
281 if (throw_error) {
282 HigMessageDialog.RunHigMessageDialog (settings_dialog,
283 DialogFlags.Modal,
284 MessageType.Warning,
285 ButtonsType.Ok,
286 Catalog.GetString ("Path not added"),
287 error_message);
288 } else {
289 // Confirm the removal of obsolete includes
290 if (obsolete_includes.Count != 0) {
291 HigMessageDialog dialog = new HigMessageDialog (settings_dialog,
292 DialogFlags.Modal,
293 MessageType.Question,
294 ButtonsType.YesNo,
295 Catalog.GetString ("Remove obsolete paths"),
296 Catalog.GetString ("Adding this path will obsolete some of the existing include paths. " +
297 "This will result in the removal of the old obsolete paths. Do you still wish to add it?"));
299 ResponseType confirm_response = (ResponseType) dialog.Run ();
301 if (confirm_response != ResponseType.Yes)
302 return;
304 foreach (string obsolete_include in obsolete_includes)
305 include_view.RemovePath (obsolete_include);
307 dialog.Destroy ();
310 include_view.AddPath (new_include);
315 private void OnRemoveIncludeClicked (object o, EventArgs args)
317 // Confirm removal
318 HigMessageDialog dialog = new HigMessageDialog (settings_dialog,
319 DialogFlags.Modal,
320 MessageType.Question,
321 ButtonsType.YesNo,
322 Catalog.GetString ("Remove path"),
323 Catalog.GetString ("Are you sure you wish to remove this path from the list of directories to be included for indexing?"));
324 ResponseType response = (ResponseType) dialog.Run ();
325 dialog.Destroy ();
327 if (response != ResponseType.Yes)
328 return;
330 include_view.RemoveSelectedPath ();
331 remove_include_button.Sensitive = false;
334 private void OnIncludeSelected (object o, EventArgs args)
336 remove_include_button.Sensitive = true;
339 private void OnAddExcludeClicked (object o, EventArgs args)
341 AddExcludeDialog dialog = new AddExcludeDialog (settings_dialog);
342 dialog.ExcludeItemAddedEvent += new ExcludeItemAddedHandler (OnExcludeItemAdded);
346 private void OnRemoveExcludeClicked (object o, EventArgs args)
348 HigMessageDialog dialog = new HigMessageDialog (settings_dialog,
349 DialogFlags.Modal,
350 MessageType.Question,
351 ButtonsType.YesNo,
352 Catalog.GetString ("Remove item"),
353 Catalog.GetString ("Are you sure you wish to remove this item from the list of data to be excluded from indexing?"));
355 ResponseType response = (ResponseType) dialog.Run ();
356 dialog.Destroy ();
358 if (response != ResponseType.Yes)
359 return;
361 exclude_view.RemoveSelectedItem ();
362 remove_exclude_button.Sensitive = false;
365 private void OnExcludeSelected (object o, EventArgs args)
367 remove_exclude_button.Sensitive = true;
370 private void OnExcludeItemAdded (ExcludeItem exclude_item)
372 exclude_view.AddItem (exclude_item);
375 //Netbeagle: Add, Remove operations
376 private void OnAddNetbeagleClicked (object o, EventArgs args)
378 Glade.XML glade = new Glade.XML (null, "settings.glade", "netbeagle_entry_dialog", "beagle");
379 glade.Autoconnect (this);
380 ResponseType nbed_response = (ResponseType) netbeagle_entry_dialog.Run ();
382 string new_node = netbeagle_textentry.Text.Trim();
384 if ((new_node.Length > 1) && (((string[])new_node.Split(':')).Length < 2))
385 new_node += ":8888";
387 netbeagle_entry_dialog.Destroy ();
389 if (nbed_response == ResponseType.Ok) {
390 string error_message = "";
391 bool throw_error = false;
393 if (new_node.Length < 2)
395 throw_error = true;
396 error_message = Catalog.GetString ("Invalid host entry");
398 else
399 // Check if the new entry matches an existing netbeagle entry
400 foreach (string old_node in netbeagle_view.Netbeagles) {
401 if (new_node == old_node) {
402 throw_error = true;
403 error_message = Catalog.GetString ("Remote host already present in the list.");
407 if (throw_error) {
408 HigMessageDialog.RunHigMessageDialog (settings_dialog,
409 DialogFlags.Modal,
410 MessageType.Warning,
411 ButtonsType.Ok,
412 Catalog.GetString ("Netbeagle Node not added"),
413 error_message);
415 else
416 netbeagle_view.AddNode (new_node);
421 private void OnRemoveNetbeagleClicked (object o, EventArgs args)
423 // Confirm removal
424 HigMessageDialog dialog = new HigMessageDialog (settings_dialog,
425 DialogFlags.Modal,
426 MessageType.Question,
427 ButtonsType.YesNo,
428 Catalog.GetString ("Remove host"),
429 Catalog.GetString ("Are you sure you wish to remove this host from the list?"));
431 ResponseType response = (ResponseType) dialog.Run ();
432 dialog.Destroy ();
434 if (response != ResponseType.Yes)
435 return;
437 netbeagle_view.RemoveSelectedNode ();
439 remove_netbeagle_button.Sensitive = false;
442 private void OnNetbeagleSelected (object o, EventArgs args)
444 remove_netbeagle_button.Sensitive = true;
447 //Publicfolders: Add, Remove operations
448 private void OnAddPublicfolderClicked (object o, EventArgs args)
450 CompatFileChooserDialog fs_dialog = new CompatFileChooserDialog (Catalog.GetString ("Select path"),
451 settings_dialog,
452 CompatFileChooserDialog.Action.SelectFolder);
453 fs_dialog.SelectMultiple = false;
455 ResponseType fs_response = (ResponseType) fs_dialog.Run ();
456 string new_pf = fs_dialog.Filename;
457 fs_dialog.Destroy ();
459 if (fs_response == ResponseType.Ok) {
461 string error_message = "";
462 bool throw_error = false;
464 // Check and see if the current data collides with the new path in any way
465 // FIXME: Do this with System.IO.Path or something
466 foreach (string old_pf in publicfolder_view.Publicfolders) {
467 if (new_pf == old_pf) {
468 throw_error = true;
469 error_message = Catalog.GetString ("The selected path is already configured for external access.");
473 if (throw_error) {
474 HigMessageDialog.RunHigMessageDialog (settings_dialog,
475 DialogFlags.Modal,
476 MessageType.Warning,
477 ButtonsType.Ok,
478 Catalog.GetString ("Path not added"),
479 error_message);
481 else
482 publicfolder_view.AddPath (new_pf);
486 private void OnRemovePublicfolderClicked (object o, EventArgs args)
488 // Confirm removal
489 HigMessageDialog dialog = new HigMessageDialog (settings_dialog,
490 DialogFlags.Modal,
491 MessageType.Question,
492 ButtonsType.YesNo,
493 Catalog.GetString ("Remove public path"),
494 Catalog.GetString ("Are you sure you wish to remove this entry from the list of public paths?"));
496 ResponseType response = (ResponseType) dialog.Run ();
497 dialog.Destroy ();
499 if (response != ResponseType.Yes)
500 return;
502 publicfolder_view.RemoveSelectedPath ();
503 remove_publicfolder_button.Sensitive = false;
506 private void OnPublicfolderSelected (object o, EventArgs args)
508 remove_publicfolder_button.Sensitive = true;
511 ////////////////////////////////////////////////////////////////
512 // IncludeView
514 class IncludeView : TreeView
516 private ListStore store;
518 private ArrayList includes = new ArrayList ();
520 public ArrayList Includes {
521 get { return includes; }
524 private enum TargetType {
525 Uri,
528 private static TargetEntry [] target_table = new TargetEntry [] {
529 new TargetEntry ("STRING", 0, (uint) TargetType.Uri ),
530 new TargetEntry ("text/plain", 0, (uint) TargetType.Uri),
533 public IncludeView ()
535 store = new ListStore (typeof (string));
537 this.Model = store;
539 AppendColumn (Catalog.GetString ("Name"), new CellRendererText (), "text", 0);
541 // Enable drag and drop folders from nautilus
542 Gtk.Drag.DestSet (this, DestDefaults.All, target_table, DragAction.Copy | DragAction.Move);
543 DragDataReceived += new DragDataReceivedHandler (HandleData);
546 public void AddPath (string path)
548 includes.Add (path);
549 store.AppendValues (path);
552 public void RemovePath (string path)
554 find_path = path;
555 found_iter = TreeIter.Zero;
557 this.Model.Foreach (new TreeModelForeachFunc (ForeachFindPath));
559 store.Remove (ref found_iter);
560 includes.Remove (path);
563 private string find_path;
564 private TreeIter found_iter;
566 private bool ForeachFindPath (TreeModel model, TreePath path, TreeIter iter)
568 if ((string) model.GetValue (iter, 0) == find_path) {
569 found_iter = iter;
570 return true;
573 return false;
576 public void RemoveSelectedPath ()
578 TreeModel model;
579 TreeIter iter;
581 if (!this.Selection.GetSelected(out model, out iter)) {
582 return;
584 string path = (string)model.GetValue(iter, 0);
586 store.Remove (ref iter);
587 includes.Remove (path);
590 // Handle drag and drop data. Enables users to drag a folder that he wishes
591 // to add for indexing from Nautilus.
592 // FIXME: Pass checks as in OnAddIncludeButtonClicked
593 private void HandleData (object o, DragDataReceivedArgs args) {
594 Uri uri;
595 if (args.SelectionData.Length >=0 && args.SelectionData.Format == 8) {
596 uri = new Uri (args.SelectionData.Text.Trim ());
597 AddPath (uri.LocalPath);
598 Gtk.Drag.Finish (args.Context, true, false, args.Time);
600 Gtk.Drag.Finish (args.Context, false, false, args.Time);
604 ////////////////////////////////////////////////////////////////
605 // Exclude view
607 class ExcludeView : TreeView
609 ArrayList excludes = new ArrayList ();
611 public ArrayList Excludes {
612 get { return excludes; }
615 public ExcludeView ()
617 this.Model = new ListStore (typeof (ExcludeItem));
619 CellRendererText renderer_text = new CellRendererText ();
621 TreeViewColumn type_column = new TreeViewColumn ();
622 type_column.Title = Catalog.GetString ("Type");
623 type_column.PackStart (renderer_text, false);
624 type_column.SetCellDataFunc (renderer_text, new TreeCellDataFunc (TypeCellDataFunc));
625 AppendColumn (type_column);
627 TreeViewColumn name_column = new TreeViewColumn ();
628 name_column.Title = Catalog.GetString ("Name");
629 name_column.PackStart (renderer_text, false);
630 name_column.SetCellDataFunc (renderer_text, new TreeCellDataFunc (NameCellDataFunc));
631 AppendColumn (name_column);
634 public void RemoveSelectedItem ()
636 TreeModel model;
637 TreeIter iter;
639 if (!this.Selection.GetSelected(out model, out iter)) {
640 return;
642 ExcludeItem exclude_item = (ExcludeItem) model.GetValue(iter, 0);
644 ((ListStore)this.Model).Remove (ref iter);
645 excludes.Remove (exclude_item);
648 public void AddItem (ExcludeItem exclude_item)
650 excludes.Add (exclude_item);
651 ((ListStore)this.Model).AppendValues (exclude_item);
654 private void NameCellDataFunc (TreeViewColumn column,
655 CellRenderer renderer,
656 TreeModel model,
657 TreeIter iter)
659 ExcludeItem exclude_item = (ExcludeItem) model.GetValue (iter, 0);
660 if (exclude_item.Type == ExcludeType.MailFolder)
661 ((CellRendererText)renderer).Text = MailFolder.GetNameForPath (exclude_item.Value);
662 else
663 ((CellRendererText)renderer).Text = exclude_item.Value;
666 private void TypeCellDataFunc (TreeViewColumn column,
667 CellRenderer renderer,
668 TreeModel model,
669 TreeIter iter)
671 ExcludeItem exclude_item = (ExcludeItem) model.GetValue (iter, 0);
673 switch (exclude_item.Type) {
674 case ExcludeType.Path:
675 ((CellRendererText)renderer).Text = Catalog.GetString ("Path");
676 break;
677 case ExcludeType.Pattern:
678 ((CellRendererText)renderer).Text = Catalog.GetString ("Pattern");
679 break;
680 case ExcludeType.MailFolder:
681 ((CellRendererText)renderer).Text = Catalog.GetString ("Mail Folder");
682 break;
687 ////////////////////////////////////////////////////////////////
689 // NetbeagleView
690 class NetbeagleView : TreeView
692 private ListStore store;
694 private ArrayList netBeagleList = new ArrayList ();
696 public ArrayList Netbeagles {
697 get { return netBeagleList; }
700 public NetbeagleView ()
702 store = new ListStore (typeof (string));
704 this.Model = store;
706 AppendColumn (Catalog.GetString ("Name"), new CellRendererText (), "text", 0);
709 public void AddNode (string node)
711 netBeagleList.Add (node);
712 store.AppendValues (node);
715 public void RemoveNode (string node)
717 find_node = node;
718 found_iter = TreeIter.Zero;
720 this.Model.Foreach (new TreeModelForeachFunc (ForeachFindNode));
722 store.Remove (ref found_iter);
723 netBeagleList.Remove (node);
726 private string find_node;
727 private TreeIter found_iter;
729 private bool ForeachFindNode (TreeModel model, TreePath path, TreeIter iter)
731 if ((string) model.GetValue (iter, 0) == find_node) {
732 found_iter = iter;
733 return true;
736 return false;
739 public void RemoveSelectedNode ()
741 TreeModel model;
742 TreeIter iter;
744 if (!this.Selection.GetSelected(out model, out iter)) {
745 return;
747 string node = (string)model.GetValue(iter, 0);
749 store.Remove (ref iter);
750 netBeagleList.Remove (node);
754 ////////////////////////////////////////////////////////////////
755 // PublicfolderView
757 class PublicfolderView: TreeView
759 private ListStore store;
761 private ArrayList publicFolders = new ArrayList ();
763 public ArrayList Publicfolders {
764 get { return publicFolders; }
767 private enum TargetType {
768 Uri,
771 private static TargetEntry [] target_table = new TargetEntry [] {
772 new TargetEntry ("STRING", 0, (uint) TargetType.Uri ),
773 new TargetEntry ("text/plain", 0, (uint) TargetType.Uri),
776 public PublicfolderView ()
778 store = new ListStore (typeof (string));
780 this.Model = store;
782 AppendColumn (Catalog.GetString ("Name"), new CellRendererText (), "text", 0);
784 // Enable drag and drop folders from nautilus
785 Gtk.Drag.DestSet (this, DestDefaults.All, target_table, DragAction.Copy | DragAction.Move);
786 DragDataReceived += new DragDataReceivedHandler (HandleData);
789 public void AddPath (string path)
791 publicFolders.Add (path);
792 store.AppendValues (path);
795 public void RemovePath (string path)
797 find_path = path;
798 found_iter = TreeIter.Zero;
800 this.Model.Foreach (new TreeModelForeachFunc (ForeachFindPath));
802 store.Remove (ref found_iter);
803 publicFolders.Remove (path);
806 private string find_path;
807 private TreeIter found_iter;
809 private bool ForeachFindPath (TreeModel model, TreePath path, TreeIter iter)
811 if ((string) model.GetValue (iter, 0) == find_path) {
812 found_iter = iter;
813 return true;
816 return false;
819 public void RemoveSelectedPath ()
821 TreeModel model;
822 TreeIter iter;
824 if (!this.Selection.GetSelected(out model, out iter)) {
825 return;
827 string path = (string)model.GetValue(iter, 0);
829 store.Remove (ref iter);
830 publicFolders.Remove (path);
833 // Handle drag and drop data. Enables users to drag a folder that he wishes
834 // to add for indexing from Nautilus.
835 // FIXME: Pass checks as in OnAddIncludeButtonClicked
836 private void HandleData (object o, DragDataReceivedArgs args) {
837 Uri uri;
838 if (args.SelectionData.Length >=0 && args.SelectionData.Format == 8) {
839 uri = new Uri (args.SelectionData.Text.Trim ());
840 AddPath (uri.LocalPath);
841 Gtk.Drag.Finish (args.Context, true, false, args.Time);
843 Gtk.Drag.Finish (args.Context, false, false, args.Time);
847 ////////////////////////////////////////////////////////////////
848 // Mail folder dialog
850 class MailFolderDialog
852 Gtk.Window parent;
853 FolderView folder_view;
855 [Widget] Dialog mail_folder_dialog;
856 [Widget] ScrolledWindow folder_sw;
858 public event ExcludeItemAddedHandler ExcludeItemAddedEvent;
860 public MailFolderDialog (Gtk.Window parent)
862 this.parent = parent;
864 Glade.XML glade = new Glade.XML (null, "settings.glade", "mail_folder_dialog", "beagle");
865 glade.Autoconnect (this);
867 folder_view = new FolderView ();
868 folder_view.Show ();
869 folder_sw.Child = folder_view;
872 private void OnDialogResponse (object o, ResponseArgs args)
874 if (args.ResponseId == ResponseType.Cancel) {
875 mail_folder_dialog.Destroy ();
876 return;
879 ExcludeItem exclude_item;
880 object obj = folder_view.GetCurrentItem ();
882 if (obj is MailAccount) {
884 } else if (obj is MailFolder) {
885 MailFolder folder = (MailFolder) obj;
886 exclude_item = new ExcludeItem (ExcludeType.MailFolder, folder.Path);
888 if (ExcludeItemAddedEvent != null)
889 ExcludeItemAddedEvent (exclude_item);
891 mail_folder_dialog.Destroy ();
895 class FolderView : TreeView
897 TreeStore store;
898 Gdk.Pixbuf folder_icon;
900 public FolderView ()
902 store = new TreeStore (typeof (MailFolder));
903 this.Model = store;
905 folder_icon = this.RenderIcon (Stock.Open, IconSize.Menu, "");
907 HeadersVisible = false;
909 TreeViewColumn column = new TreeViewColumn ();
910 CellRendererPixbuf renderer_icon = new CellRendererPixbuf ();
911 column.PackStart (renderer_icon, false);
912 column.SetCellDataFunc (renderer_icon, new TreeCellDataFunc (IconCellDataFunc));
914 CellRendererText renderer_text = new CellRendererText ();
915 column.PackStart (renderer_text, false);
916 column.SetCellDataFunc (renderer_text, new TreeCellDataFunc (NameCellDataFunc));
917 AppendColumn (column);
919 foreach (MailAccount account in Beagle.Util.Evolution.Accounts) {
920 TreeIter iter = store.AppendValues (account);
922 foreach (MailFolder folder in account.Children) {
923 Add (iter, folder);
928 private void Add (TreeIter parent, MailFolder folder)
930 TreeIter current = store.AppendValues (parent, folder);
932 foreach (MailFolder child in folder.Children)
933 Add (current, child);
936 private void IconCellDataFunc (TreeViewColumn column,
937 CellRenderer renderer,
938 TreeModel model,
939 TreeIter iter)
941 object obj = model.GetValue (iter, 0);
942 ((CellRendererPixbuf)renderer).Pixbuf = (obj is MailAccount) ? null : folder_icon;
945 private void NameCellDataFunc (TreeViewColumn column,
946 CellRenderer renderer,
947 TreeModel model,
948 TreeIter iter)
950 object obj = model.GetValue (iter, 0);
952 if (obj is MailAccount) {
953 MailAccount account = obj as MailAccount;
954 ((CellRendererText)renderer).Markup = String.Format ("<b>{0}</b>",account.Name);
955 } else {
956 MailFolder folder = obj as MailFolder;
957 ((CellRendererText)renderer).Text = folder.Name;
961 public MailFolder GetCurrentItem ()
963 TreeModel model;
964 TreeIter iter;
966 if (!this.Selection.GetSelected (out model, out iter)) {
967 return null;
970 return (MailFolder) model.GetValue (iter, 0);
975 ////////////////////////////////////////////////////////////////
976 // Exclude dialog
978 private delegate void ExcludeItemAddedHandler (ExcludeItem item);
980 class AddExcludeDialog
982 Gtk.Window parent;
984 [Widget] Dialog add_exclude_dialog;
985 [Widget] Entry value_entry;
986 [Widget] Label value_name_label;
987 [Widget] Button browse_button;
989 [Widget] RadioButton type_path_radio;
990 [Widget] RadioButton type_pattern_radio;
991 [Widget] RadioButton type_mailfolder_radio;
993 public event ExcludeItemAddedHandler ExcludeItemAddedEvent;
995 private string value;
997 public string Value {
998 get {
999 if (Type == ExcludeType.MailFolder)
1000 return value;
1001 else
1002 return value_entry.Text;
1006 public ExcludeType Type {
1007 get {
1008 if (type_path_radio.Active)
1009 return ExcludeType.Path;
1010 else if (type_pattern_radio.Active)
1011 return ExcludeType.Pattern;
1012 else
1013 return ExcludeType.MailFolder;
1017 public AddExcludeDialog (Gtk.Window parent)
1019 this.parent = parent;
1021 Glade.XML glade = new Glade.XML (null, "settings.glade", "add_exclude_dialog", "beagle");
1022 glade.Autoconnect (this);
1025 private void OnBrowseButtonClicked (object o, EventArgs args)
1027 switch (Type) {
1028 case ExcludeType.Path:
1029 CompatFileChooserDialog fs_dialog = new CompatFileChooserDialog (Catalog.GetString ("Select Folder"),
1030 add_exclude_dialog,
1031 CompatFileChooserDialog.Action.SelectFolder);
1032 fs_dialog.SelectMultiple = false;
1034 ResponseType response = (ResponseType) fs_dialog.Run ();
1036 if (response == ResponseType.Ok)
1037 value_entry.Text = fs_dialog.Filename;
1039 fs_dialog.Destroy ();
1040 break;
1041 case ExcludeType.MailFolder:
1042 MailFolderDialog mf_dialog = new MailFolderDialog (add_exclude_dialog);
1043 mf_dialog.ExcludeItemAddedEvent += new ExcludeItemAddedHandler (OnExcludeItemAdded);
1044 break;
1048 private void OnRadioGroupChanged (object o, EventArgs args)
1050 value_entry.Text = "";
1052 switch (Type) {
1053 case ExcludeType.Path:
1054 browse_button.Sensitive = true;
1055 value_name_label.Text = Catalog.GetString ("Path");
1056 value_entry.IsEditable = true;
1057 break;
1058 case ExcludeType.MailFolder:
1059 browse_button.Sensitive = true;
1060 value_name_label.Text = Catalog.GetString ("Mail Folder");
1061 value_entry.IsEditable = false;
1062 break;
1063 case ExcludeType.Pattern:
1064 browse_button.Sensitive = false;
1065 value_name_label.Text = Catalog.GetString ("Pattern");
1066 value_entry.IsEditable = true;
1067 break;
1071 private void OnExcludeItemAdded (ExcludeItem item)
1073 value = item.Value;
1074 value_entry.Text = MailFolder.GetNameForPath (item.Value);
1077 private void OnDialogResponse (object o, ResponseArgs args)
1079 if (((ResponseType)args.ResponseId) == ResponseType.Ok) {
1080 ExcludeItem exclude_item = new ExcludeItem (Type, Value);
1082 switch (Type) {
1083 case ExcludeType.Path:
1084 if (!Directory.Exists (Value)) {
1085 HigMessageDialog.RunHigMessageDialog(add_exclude_dialog,
1086 DialogFlags.Modal,
1087 MessageType.Error,
1088 ButtonsType.Ok,
1089 Catalog.GetString ("Error adding path"),
1090 Catalog.GetString ("The specified path could not be found and therefore it could not be added to the list of resources excluded for indexing."));
1091 return;
1093 break;
1096 if (ExcludeItemAddedEvent != null)
1097 ExcludeItemAddedEvent (exclude_item);
1099 add_exclude_dialog.Destroy ();
1102 public void Destroy ()
1104 add_exclude_dialog.Destroy ();
1108 #if false
1109 ////////////////////////////////////////////////////////////////
1110 // Display view
1112 class DisplayView : TreeView
1114 public DisplayView ()
1116 ListStore store = new ListStore (typeof(bool),typeof(string));
1118 this.Model = store;
1120 AppendColumn (Catalog.GetString ("Show"), new CellRendererToggle (), "active", 0);
1121 AppendColumn (Catalog.GetString ("Name"), new CellRendererText (), "text", 1);
1124 #endif