Add Konqueror bookmark backend.
[beagle.git] / tools / Settings.cs
blob8ee1f4044c05f7a54cf29abf60778ec36d3521c3
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 Glade;
35 using Mono.Unix;
37 using Beagle;
38 using Beagle.Util;
40 public class SettingsDialog
42 public static void Main (string[] args)
44 SettingsDialog settings = new SettingsDialog ();
45 settings.Run ();
48 ////////////////////////////////////////////////////////////////
49 // Widgets
51 [Widget] VBox administration_frame;
52 [Widget] CheckButton allow_root_toggle;
54 [Widget] CheckButton autostart_toggle;
55 [Widget] CheckButton battery_toggle;
57 [Widget] CheckButton press_ctrl_toggle;
58 [Widget] CheckButton press_alt_toggle;
59 [Widget] Entry show_search_window_entry;
60 [Widget] SpinButton max_displayed_spinbutton;
62 [Widget] CheckButton index_home_toggle;
63 [Widget] Button remove_include_button;
64 [Widget] Button remove_exclude_button;
66 [Widget] Button display_up_button;
67 [Widget] Button display_down_button;
69 [Widget] Gtk.Window settings_dialog;
71 [Widget] ScrolledWindow display_sw;
73 [Widget] ScrolledWindow include_sw;
74 [Widget] ScrolledWindow exclude_sw;
76 #if false
77 DisplayView display_view;
78 #endif
80 IncludeView include_view;
81 ExcludeView exclude_view;
83 ////////////////////////////////////////////////////////////////
84 // WebServices
86 [Widget] VBox networking_box;
87 [Widget] CheckButton allow_global_access_toggle;
88 [Widget] Button remove_publicfolder_button;
89 [Widget] Button remove_netbeagle_button;
91 [Widget] ScrolledWindow publicfolder_sw;
92 [Widget] ScrolledWindow netbeagle_sw;
94 [Widget] Dialog netbeagle_entry_dialog;
95 [Widget] Entry netbeagle_textentry;
97 PublicfolderView publicfolder_view;
98 NetbeagleView netbeagle_view;
100 ////////////////////////////////////////////////////////////////
101 // Initialize
103 public SettingsDialog ()
105 Application.Init ();
107 Catalog.Init ("beagle", ExternalStringsHack.LocaleDir);
109 Glade.XML glade = new Glade.XML (null, "settings.glade", "settings_dialog", "beagle");
110 glade.Autoconnect (this);
112 settings_dialog.Icon = Beagle.Images.GetPixbuf ("system-search.png");
113 administration_frame.Visible = (Environment.UserName == "root");
115 #if false
116 display_view = new DisplayView ();
117 display_view.Selection.Changed += new EventHandler (OnDisplaySelected);
118 display_view.Show ();
119 // The display sorting stuff is something i have been testing locally, Best CVS wont use it
120 display_sw.Child = display_view;
121 #endif
122 include_view = new IncludeView ();
123 include_view.Selection.Changed += new EventHandler (OnIncludeSelected);
124 include_view.Show ();
125 include_sw.Child = include_view;
127 exclude_view = new ExcludeView ();
128 exclude_view.Selection.Changed += new EventHandler (OnExcludeSelected);
129 exclude_view.Show ();
130 exclude_sw.Child = exclude_view;
132 #if ENABLE_WEBSERVICES
133 networking_box.Visible = true;
135 publicfolder_view = new PublicfolderView ();
136 publicfolder_view.Selection.Changed += new EventHandler (OnPublicfolderSelected);
137 publicfolder_view.Show ();
138 publicfolder_sw.Child = publicfolder_view;
140 netbeagle_view = new NetbeagleView ();
141 netbeagle_view.Selection.Changed += new EventHandler (OnNetbeagleSelected);
142 netbeagle_view.Show ();
143 netbeagle_sw.Child = netbeagle_view;
144 #endif
145 LoadConfiguration ();
147 Conf.Subscribe (typeof (Conf.IndexingConfig), new Conf.ConfigUpdateHandler (OnConfigurationChanged));
148 Conf.Subscribe (typeof (Conf.SearchingConfig), new Conf.ConfigUpdateHandler (OnConfigurationChanged));
150 #if ENABLE_WEBSERVICES
151 Conf.Subscribe (typeof (Conf.NetworkingConfig), new Conf.ConfigUpdateHandler (OnConfigurationChanged));
152 Conf.Subscribe (typeof (Conf.WebServicesConfig), new Conf.ConfigUpdateHandler (OnConfigurationChanged));
153 #endif
156 public void Run ()
158 Application.Run ();
161 ////////////////////////////////////////////////////////////////
162 // Configuration
164 private void LoadConfiguration ()
166 allow_root_toggle.Active = Conf.Daemon.AllowRoot;
167 autostart_toggle.Active = Conf.Searching.Autostart;
168 battery_toggle.Active = Conf.Indexing.IndexOnBattery;
170 KeyBinding show_binding = Conf.Searching.ShowSearchWindowBinding;
171 press_ctrl_toggle.Active = show_binding.Ctrl;
172 press_alt_toggle.Active = show_binding.Alt;
173 show_search_window_entry.Text = show_binding.Key;
174 max_displayed_spinbutton.Value = (uint) Conf.Searching.MaxDisplayed;
176 if (Conf.Indexing.IndexHomeDir)
177 index_home_toggle.Active = true;
179 foreach (string include in Conf.Indexing.Roots)
180 include_view.AddPath (include);
182 foreach (ExcludeItem exclude_item in Conf.Indexing.Excludes)
183 exclude_view.AddItem (exclude_item);
185 #if ENABLE_WEBSERVICES
186 foreach (string netbeagle in Conf.Networking.NetBeagleNodes)
187 netbeagle_view.AddNode (netbeagle);
189 if (Conf.WebServices.AllowGlobalAccess)
190 allow_global_access_toggle.Active = true;
192 foreach (string publicfolder in Conf.WebServices.PublicFolders)
193 publicfolder_view.AddPath (publicfolder);
194 #endif
197 private void SaveConfiguration ()
199 Conf.Daemon.AllowRoot = allow_root_toggle.Active;
200 Conf.Searching.Autostart = autostart_toggle.Active;
201 Conf.Indexing.IndexOnBattery = battery_toggle.Active;
203 Conf.Searching.ShowSearchWindowBinding = new KeyBinding (show_search_window_entry.Text,
204 press_ctrl_toggle.Active,
205 press_alt_toggle.Active);
207 Conf.Searching.MaxDisplayed = (int) max_displayed_spinbutton.Value;
208 Conf.Indexing.IndexHomeDir = index_home_toggle.Active;
210 Conf.Indexing.Roots = include_view.Includes;
211 Conf.Indexing.Excludes = exclude_view.Excludes;
213 #if ENABLE_WEBSERVICES
214 Conf.Networking.NetBeagleNodes = netbeagle_view.Netbeagles;
215 Conf.WebServices.AllowGlobalAccess = allow_global_access_toggle.Active;
216 Conf.WebServices.PublicFolders = publicfolder_view.Publicfolders;
217 #endif
218 Conf.Save (true);
221 private void OnConfigurationChanged (Conf.Section section)
223 HigMessageDialog dialog = new HigMessageDialog (settings_dialog,
224 DialogFlags.Modal,
225 MessageType.Question,
226 ButtonsType.YesNo,
227 Catalog.GetString ("Reload configuration"),
228 Catalog.GetString ("The configuration file has been modified by another application. " +
229 "Do you wish to discard the currently displayed values and reload configuration from disk?"));
231 ResponseType response = (ResponseType) dialog.Run ();
233 if (response == ResponseType.Yes)
234 LoadConfiguration ();
236 dialog.Destroy ();
239 ////////////////////////////////////////////////////////////////
240 // Eventhandlers
242 private void OnDialogResponse (object o, ResponseArgs args)
244 switch (args.ResponseId) {
245 case ResponseType.Help:
246 Gnome.Url.Show ("http://beagle-project.org/Configuring");
247 break;
248 case ResponseType.Ok:
249 SaveConfiguration ();
250 Application.Quit ();
251 break;
252 default:
253 Application.Quit ();
254 break;
258 private void OnDisplaySelected (object o, EventArgs args)
260 display_up_button.Sensitive = true;
261 display_down_button.Sensitive = true;
264 private void OnAddIncludeClicked (object o, EventArgs args)
266 CompatFileChooserDialog fs_dialog = new CompatFileChooserDialog (Catalog.GetString ("Select Path"),
267 settings_dialog,
268 CompatFileChooserDialog.Action.SelectFolder);
269 fs_dialog.SelectMultiple = false;
271 ResponseType fs_response = (ResponseType) fs_dialog.Run ();
272 string new_include = fs_dialog.Filename;
273 fs_dialog.Destroy ();
275 if (fs_response == ResponseType.Ok) {
276 string error_message = "";
277 bool throw_error = false;
278 ArrayList obsolete_includes = new ArrayList ();
280 // Check and see if the current data collides with the new path in any way
281 // FIXME: Do this with System.IO.Path or something
282 foreach (string old_include in include_view.Includes) {
283 if (new_include == old_include) {
284 throw_error = true;
285 error_message = Catalog.GetString ("The selected path is already selected for indexing and wasn't added.");
286 } else if (new_include.StartsWith (old_include)) {
287 throw_error = true;
288 error_message = Catalog.GetString ("The selected path wasn't added. The list contains items that supercedes it and the data is already being indexed.");
289 } else if (old_include.StartsWith (new_include)) {
290 obsolete_includes.Add (old_include);
294 if (throw_error) {
295 HigMessageDialog.RunHigMessageDialog (settings_dialog,
296 DialogFlags.Modal,
297 MessageType.Warning,
298 ButtonsType.Ok,
299 Catalog.GetString ("Path not added"),
300 error_message);
301 } else {
302 // Confirm the removal of obsolete includes
303 if (obsolete_includes.Count != 0) {
304 HigMessageDialog dialog = new HigMessageDialog (settings_dialog,
305 DialogFlags.Modal,
306 MessageType.Question,
307 ButtonsType.YesNo,
308 Catalog.GetString ("Remove obsolete paths"),
309 Catalog.GetString ("Adding this path will obsolete some of the existing include paths. " +
310 "This will result in the removal of the old obsolete paths. Do you still wish to add it?"));
312 ResponseType confirm_response = (ResponseType) dialog.Run ();
314 if (confirm_response != ResponseType.Yes)
315 return;
317 foreach (string obsolete_include in obsolete_includes)
318 include_view.RemovePath (obsolete_include);
320 dialog.Destroy ();
323 include_view.AddPath (new_include);
328 private void OnRemoveIncludeClicked (object o, EventArgs args)
330 // Confirm removal
331 HigMessageDialog dialog = new HigMessageDialog (settings_dialog,
332 DialogFlags.Modal,
333 MessageType.Question,
334 ButtonsType.YesNo,
335 Catalog.GetString ("Remove path"),
336 Catalog.GetString ("Are you sure you wish to remove this path from the list of directories to be included for indexing?"));
337 ResponseType response = (ResponseType) dialog.Run ();
338 dialog.Destroy ();
340 if (response != ResponseType.Yes)
341 return;
343 include_view.RemoveSelectedPath ();
344 remove_include_button.Sensitive = false;
347 private void OnIncludeSelected (object o, EventArgs args)
349 remove_include_button.Sensitive = true;
352 private void OnAddExcludeClicked (object o, EventArgs args)
354 AddExcludeDialog dialog = new AddExcludeDialog (settings_dialog);
355 dialog.ExcludeItemAddedEvent += new ExcludeItemAddedHandler (OnExcludeItemAdded);
359 private void OnRemoveExcludeClicked (object o, EventArgs args)
361 HigMessageDialog dialog = new HigMessageDialog (settings_dialog,
362 DialogFlags.Modal,
363 MessageType.Question,
364 ButtonsType.YesNo,
365 Catalog.GetString ("Remove item"),
366 Catalog.GetString ("Are you sure you wish to remove this item from the list of data to be excluded from indexing?"));
368 ResponseType response = (ResponseType) dialog.Run ();
369 dialog.Destroy ();
371 if (response != ResponseType.Yes)
372 return;
374 exclude_view.RemoveSelectedItem ();
375 remove_exclude_button.Sensitive = false;
378 private void OnExcludeSelected (object o, EventArgs args)
380 remove_exclude_button.Sensitive = true;
383 private void OnExcludeItemAdded (ExcludeItem exclude_item)
385 exclude_view.AddItem (exclude_item);
388 //Netbeagle: Add, Remove operations
389 private void OnAddNetbeagleClicked (object o, EventArgs args)
391 Glade.XML glade = new Glade.XML (null, "settings.glade", "netbeagle_entry_dialog", "beagle");
392 glade.Autoconnect (this);
393 ResponseType nbed_response = (ResponseType) netbeagle_entry_dialog.Run ();
395 string new_node = netbeagle_textentry.Text.Trim();
397 if ((new_node.Length > 1) && (((string[])new_node.Split(':')).Length < 2))
398 new_node += ":8888";
400 netbeagle_entry_dialog.Destroy ();
402 if (nbed_response == ResponseType.Ok) {
403 string error_message = "";
404 bool throw_error = false;
406 if (new_node.Length < 2)
408 throw_error = true;
409 error_message = Catalog.GetString ("Invalid host entry");
411 else
412 // Check if the new entry matches an existing netbeagle entry
413 foreach (string old_node in netbeagle_view.Netbeagles) {
414 if (new_node == old_node) {
415 throw_error = true;
416 error_message = Catalog.GetString ("Remote host already present in the list.");
420 if (throw_error) {
421 HigMessageDialog.RunHigMessageDialog (settings_dialog,
422 DialogFlags.Modal,
423 MessageType.Warning,
424 ButtonsType.Ok,
425 Catalog.GetString ("Netbeagle Node not added"),
426 error_message);
428 else
429 netbeagle_view.AddNode (new_node);
434 private void OnRemoveNetbeagleClicked (object o, EventArgs args)
436 // Confirm removal
437 HigMessageDialog dialog = new HigMessageDialog (settings_dialog,
438 DialogFlags.Modal,
439 MessageType.Question,
440 ButtonsType.YesNo,
441 Catalog.GetString ("Remove host"),
442 Catalog.GetString ("Are you sure you wish to remove this host from the list?"));
444 ResponseType response = (ResponseType) dialog.Run ();
445 dialog.Destroy ();
447 if (response != ResponseType.Yes)
448 return;
450 netbeagle_view.RemoveSelectedNode ();
452 remove_netbeagle_button.Sensitive = false;
455 private void OnNetbeagleSelected (object o, EventArgs args)
457 remove_netbeagle_button.Sensitive = true;
460 //Publicfolders: Add, Remove operations
461 private void OnAddPublicfolderClicked (object o, EventArgs args)
463 CompatFileChooserDialog fs_dialog = new CompatFileChooserDialog (Catalog.GetString ("Select path"),
464 settings_dialog,
465 CompatFileChooserDialog.Action.SelectFolder);
466 fs_dialog.SelectMultiple = false;
468 ResponseType fs_response = (ResponseType) fs_dialog.Run ();
469 string new_pf = fs_dialog.Filename;
470 fs_dialog.Destroy ();
472 if (fs_response == ResponseType.Ok) {
474 string error_message = "";
475 bool throw_error = false;
477 // Check and see if the current data collides with the new path in any way
478 // FIXME: Do this with System.IO.Path or something
479 foreach (string old_pf in publicfolder_view.Publicfolders) {
480 if (new_pf == old_pf) {
481 throw_error = true;
482 error_message = Catalog.GetString ("The selected path is already configured for external access.");
486 if (throw_error) {
487 HigMessageDialog.RunHigMessageDialog (settings_dialog,
488 DialogFlags.Modal,
489 MessageType.Warning,
490 ButtonsType.Ok,
491 Catalog.GetString ("Path not added"),
492 error_message);
494 else
495 publicfolder_view.AddPath (new_pf);
499 private void OnRemovePublicfolderClicked (object o, EventArgs args)
501 // Confirm removal
502 HigMessageDialog dialog = new HigMessageDialog (settings_dialog,
503 DialogFlags.Modal,
504 MessageType.Question,
505 ButtonsType.YesNo,
506 Catalog.GetString ("Remove public path"),
507 Catalog.GetString ("Are you sure you wish to remove this entry from the list of public paths?"));
509 ResponseType response = (ResponseType) dialog.Run ();
510 dialog.Destroy ();
512 if (response != ResponseType.Yes)
513 return;
515 publicfolder_view.RemoveSelectedPath ();
516 remove_publicfolder_button.Sensitive = false;
519 private void OnPublicfolderSelected (object o, EventArgs args)
521 remove_publicfolder_button.Sensitive = true;
524 ////////////////////////////////////////////////////////////////
525 // IncludeView
527 class IncludeView : TreeView
529 private ListStore store;
531 private ArrayList includes = new ArrayList ();
533 public ArrayList Includes {
534 get { return includes; }
537 private enum TargetType {
538 Uri,
541 private static TargetEntry [] target_table = new TargetEntry [] {
542 new TargetEntry ("STRING", 0, (uint) TargetType.Uri ),
543 new TargetEntry ("text/plain", 0, (uint) TargetType.Uri),
546 public IncludeView ()
548 store = new ListStore (typeof (string));
550 this.Model = store;
552 AppendColumn (Catalog.GetString ("Name"), new CellRendererText (), "text", 0);
554 // Enable drag and drop folders from nautilus
555 Gtk.Drag.DestSet (this, DestDefaults.All, target_table, DragAction.Copy | DragAction.Move);
556 DragDataReceived += new DragDataReceivedHandler (HandleData);
559 public void AddPath (string path)
561 includes.Add (path);
562 store.AppendValues (path);
565 public void RemovePath (string path)
567 find_path = path;
568 found_iter = TreeIter.Zero;
570 this.Model.Foreach (new TreeModelForeachFunc (ForeachFindPath));
572 store.Remove (ref found_iter);
573 includes.Remove (path);
576 private string find_path;
577 private TreeIter found_iter;
579 private bool ForeachFindPath (TreeModel model, TreePath path, TreeIter iter)
581 if ((string) model.GetValue (iter, 0) == find_path) {
582 found_iter = iter;
583 return true;
586 return false;
589 public void RemoveSelectedPath ()
591 TreeModel model;
592 TreeIter iter;
594 if (!this.Selection.GetSelected(out model, out iter)) {
595 return;
597 string path = (string)model.GetValue(iter, 0);
599 store.Remove (ref iter);
600 includes.Remove (path);
603 // Handle drag and drop data. Enables users to drag a folder that he wishes
604 // to add for indexing from Nautilus.
605 // FIXME: Pass checks as in OnAddIncludeButtonClicked
606 private void HandleData (object o, DragDataReceivedArgs args) {
607 Uri uri;
608 if (args.SelectionData.Length >=0 && args.SelectionData.Format == 8) {
609 uri = new Uri (args.SelectionData.Text.Trim ());
610 AddPath (uri.LocalPath);
611 Gtk.Drag.Finish (args.Context, true, false, args.Time);
613 Gtk.Drag.Finish (args.Context, false, false, args.Time);
617 ////////////////////////////////////////////////////////////////
618 // Exclude view
620 class ExcludeView : TreeView
622 ArrayList excludes = new ArrayList ();
624 public ArrayList Excludes {
625 get { return excludes; }
628 public ExcludeView ()
630 this.Model = new ListStore (typeof (ExcludeItem));
632 CellRendererText renderer_text = new CellRendererText ();
634 TreeViewColumn type_column = new TreeViewColumn ();
635 type_column.Title = Catalog.GetString ("Type");
636 type_column.PackStart (renderer_text, false);
637 type_column.SetCellDataFunc (renderer_text, new TreeCellDataFunc (TypeCellDataFunc));
638 AppendColumn (type_column);
640 TreeViewColumn name_column = new TreeViewColumn ();
641 name_column.Title = Catalog.GetString ("Name");
642 name_column.PackStart (renderer_text, false);
643 name_column.SetCellDataFunc (renderer_text, new TreeCellDataFunc (NameCellDataFunc));
644 AppendColumn (name_column);
647 public void RemoveSelectedItem ()
649 TreeModel model;
650 TreeIter iter;
652 if (!this.Selection.GetSelected(out model, out iter)) {
653 return;
655 ExcludeItem exclude_item = (ExcludeItem) model.GetValue(iter, 0);
657 ((ListStore)this.Model).Remove (ref iter);
658 excludes.Remove (exclude_item);
661 public void AddItem (ExcludeItem exclude_item)
663 excludes.Add (exclude_item);
664 ((ListStore)this.Model).AppendValues (exclude_item);
667 private void NameCellDataFunc (TreeViewColumn column,
668 CellRenderer renderer,
669 TreeModel model,
670 TreeIter iter)
672 ExcludeItem exclude_item = (ExcludeItem) model.GetValue (iter, 0);
673 if (exclude_item.Type == ExcludeType.MailFolder)
674 ((CellRendererText)renderer).Text = MailFolder.GetNameForPath (exclude_item.Value);
675 else
676 ((CellRendererText)renderer).Text = exclude_item.Value;
679 private void TypeCellDataFunc (TreeViewColumn column,
680 CellRenderer renderer,
681 TreeModel model,
682 TreeIter iter)
684 ExcludeItem exclude_item = (ExcludeItem) model.GetValue (iter, 0);
686 switch (exclude_item.Type) {
687 case ExcludeType.Path:
688 ((CellRendererText)renderer).Text = Catalog.GetString ("Path:");
689 break;
690 case ExcludeType.Pattern:
691 ((CellRendererText)renderer).Text = Catalog.GetString ("Pattern:");
692 break;
693 case ExcludeType.MailFolder:
694 ((CellRendererText)renderer).Text = Catalog.GetString ("Mail folder:");
695 break;
700 ////////////////////////////////////////////////////////////////
702 // NetbeagleView
703 class NetbeagleView : TreeView
705 private ListStore store;
707 private ArrayList netBeagleList = new ArrayList ();
709 public ArrayList Netbeagles {
710 get { return netBeagleList; }
713 public NetbeagleView ()
715 store = new ListStore (typeof (string));
717 this.Model = store;
719 AppendColumn (Catalog.GetString ("Name"), new CellRendererText (), "text", 0);
722 public void AddNode (string node)
724 netBeagleList.Add (node);
725 store.AppendValues (node);
728 public void RemoveNode (string node)
730 find_node = node;
731 found_iter = TreeIter.Zero;
733 this.Model.Foreach (new TreeModelForeachFunc (ForeachFindNode));
735 store.Remove (ref found_iter);
736 netBeagleList.Remove (node);
739 private string find_node;
740 private TreeIter found_iter;
742 private bool ForeachFindNode (TreeModel model, TreePath path, TreeIter iter)
744 if ((string) model.GetValue (iter, 0) == find_node) {
745 found_iter = iter;
746 return true;
749 return false;
752 public void RemoveSelectedNode ()
754 TreeModel model;
755 TreeIter iter;
757 if (!this.Selection.GetSelected(out model, out iter)) {
758 return;
760 string node = (string)model.GetValue(iter, 0);
762 store.Remove (ref iter);
763 netBeagleList.Remove (node);
767 ////////////////////////////////////////////////////////////////
768 // PublicfolderView
770 class PublicfolderView: TreeView
772 private ListStore store;
774 private ArrayList publicFolders = new ArrayList ();
776 public ArrayList Publicfolders {
777 get { return publicFolders; }
780 private enum TargetType {
781 Uri,
784 private static TargetEntry [] target_table = new TargetEntry [] {
785 new TargetEntry ("STRING", 0, (uint) TargetType.Uri ),
786 new TargetEntry ("text/plain", 0, (uint) TargetType.Uri),
789 public PublicfolderView ()
791 store = new ListStore (typeof (string));
793 this.Model = store;
795 AppendColumn (Catalog.GetString ("Name"), new CellRendererText (), "text", 0);
797 // Enable drag and drop folders from nautilus
798 Gtk.Drag.DestSet (this, DestDefaults.All, target_table, DragAction.Copy | DragAction.Move);
799 DragDataReceived += new DragDataReceivedHandler (HandleData);
802 public void AddPath (string path)
804 publicFolders.Add (path);
805 store.AppendValues (path);
808 public void RemovePath (string path)
810 find_path = path;
811 found_iter = TreeIter.Zero;
813 this.Model.Foreach (new TreeModelForeachFunc (ForeachFindPath));
815 store.Remove (ref found_iter);
816 publicFolders.Remove (path);
819 private string find_path;
820 private TreeIter found_iter;
822 private bool ForeachFindPath (TreeModel model, TreePath path, TreeIter iter)
824 if ((string) model.GetValue (iter, 0) == find_path) {
825 found_iter = iter;
826 return true;
829 return false;
832 public void RemoveSelectedPath ()
834 TreeModel model;
835 TreeIter iter;
837 if (!this.Selection.GetSelected(out model, out iter)) {
838 return;
840 string path = (string)model.GetValue(iter, 0);
842 store.Remove (ref iter);
843 publicFolders.Remove (path);
846 // Handle drag and drop data. Enables users to drag a folder that he wishes
847 // to add for indexing from Nautilus.
848 // FIXME: Pass checks as in OnAddIncludeButtonClicked
849 private void HandleData (object o, DragDataReceivedArgs args) {
850 Uri uri;
851 if (args.SelectionData.Length >=0 && args.SelectionData.Format == 8) {
852 uri = new Uri (args.SelectionData.Text.Trim ());
853 AddPath (uri.LocalPath);
854 Gtk.Drag.Finish (args.Context, true, false, args.Time);
856 Gtk.Drag.Finish (args.Context, false, false, args.Time);
860 ////////////////////////////////////////////////////////////////
861 // Mail folder dialog
863 class MailFolderDialog
865 Gtk.Window parent;
866 FolderView folder_view;
868 [Widget] Dialog mail_folder_dialog;
869 [Widget] ScrolledWindow folder_sw;
871 public event ExcludeItemAddedHandler ExcludeItemAddedEvent;
873 public MailFolderDialog (Gtk.Window parent)
875 this.parent = parent;
877 Glade.XML glade = new Glade.XML (null, "settings.glade", "mail_folder_dialog", "beagle");
878 glade.Autoconnect (this);
880 folder_view = new FolderView ();
881 folder_view.Show ();
882 folder_sw.Child = folder_view;
885 private void OnDialogResponse (object o, ResponseArgs args)
887 if (args.ResponseId == ResponseType.Cancel) {
888 mail_folder_dialog.Destroy ();
889 return;
892 ExcludeItem exclude_item;
893 object obj = folder_view.GetCurrentItem ();
895 if (obj is MailAccount) {
897 } else if (obj is MailFolder) {
898 MailFolder folder = (MailFolder) obj;
899 exclude_item = new ExcludeItem (ExcludeType.MailFolder, folder.Path);
901 if (ExcludeItemAddedEvent != null)
902 ExcludeItemAddedEvent (exclude_item);
904 mail_folder_dialog.Destroy ();
908 class FolderView : TreeView
910 TreeStore store;
911 Gdk.Pixbuf folder_icon;
913 public FolderView ()
915 store = new TreeStore (typeof (MailFolder));
916 this.Model = store;
918 folder_icon = this.RenderIcon (Stock.Open, IconSize.Menu, "");
920 HeadersVisible = false;
922 TreeViewColumn column = new TreeViewColumn ();
923 CellRendererPixbuf renderer_icon = new CellRendererPixbuf ();
924 column.PackStart (renderer_icon, false);
925 column.SetCellDataFunc (renderer_icon, new TreeCellDataFunc (IconCellDataFunc));
927 CellRendererText renderer_text = new CellRendererText ();
928 column.PackStart (renderer_text, false);
929 column.SetCellDataFunc (renderer_text, new TreeCellDataFunc (NameCellDataFunc));
930 AppendColumn (column);
932 foreach (MailAccount account in Beagle.Util.Evolution.Accounts) {
933 TreeIter iter = store.AppendValues (account);
935 foreach (MailFolder folder in account.Children) {
936 Add (iter, folder);
941 private void Add (TreeIter parent, MailFolder folder)
943 TreeIter current = store.AppendValues (parent, folder);
945 foreach (MailFolder child in folder.Children)
946 Add (current, child);
949 private void IconCellDataFunc (TreeViewColumn column,
950 CellRenderer renderer,
951 TreeModel model,
952 TreeIter iter)
954 object obj = model.GetValue (iter, 0);
955 ((CellRendererPixbuf)renderer).Pixbuf = (obj is MailAccount) ? null : folder_icon;
958 private void NameCellDataFunc (TreeViewColumn column,
959 CellRenderer renderer,
960 TreeModel model,
961 TreeIter iter)
963 object obj = model.GetValue (iter, 0);
965 if (obj is MailAccount) {
966 MailAccount account = obj as MailAccount;
967 ((CellRendererText)renderer).Markup = String.Format ("<b>{0}</b>",account.Name);
968 } else {
969 MailFolder folder = obj as MailFolder;
970 ((CellRendererText)renderer).Text = folder.Name;
974 public MailFolder GetCurrentItem ()
976 TreeModel model;
977 TreeIter iter;
979 if (!this.Selection.GetSelected (out model, out iter)) {
980 return null;
983 return (MailFolder) model.GetValue (iter, 0);
988 ////////////////////////////////////////////////////////////////
989 // Exclude dialog
991 private delegate void ExcludeItemAddedHandler (ExcludeItem item);
993 class AddExcludeDialog
995 Gtk.Window parent;
997 [Widget] Dialog add_exclude_dialog;
998 [Widget] Entry value_entry;
999 [Widget] Label value_name_label;
1000 [Widget] Button browse_button;
1002 [Widget] RadioButton type_path_radio;
1003 [Widget] RadioButton type_pattern_radio;
1004 [Widget] RadioButton type_mailfolder_radio;
1006 public event ExcludeItemAddedHandler ExcludeItemAddedEvent;
1008 private string value;
1010 public string Value {
1011 get {
1012 if (Type == ExcludeType.MailFolder)
1013 return value;
1014 else
1015 return value_entry.Text;
1019 public ExcludeType Type {
1020 get {
1021 if (type_path_radio.Active)
1022 return ExcludeType.Path;
1023 else if (type_pattern_radio.Active)
1024 return ExcludeType.Pattern;
1025 else
1026 return ExcludeType.MailFolder;
1030 public AddExcludeDialog (Gtk.Window parent)
1032 this.parent = parent;
1034 Glade.XML glade = new Glade.XML (null, "settings.glade", "add_exclude_dialog", "beagle");
1035 glade.Autoconnect (this);
1038 private void OnBrowseButtonClicked (object o, EventArgs args)
1040 switch (Type) {
1041 case ExcludeType.Path:
1042 CompatFileChooserDialog fs_dialog = new CompatFileChooserDialog (Catalog.GetString ("Select Folder"),
1043 add_exclude_dialog,
1044 CompatFileChooserDialog.Action.SelectFolder);
1045 fs_dialog.SelectMultiple = false;
1047 ResponseType response = (ResponseType) fs_dialog.Run ();
1049 if (response == ResponseType.Ok)
1050 value_entry.Text = fs_dialog.Filename;
1052 fs_dialog.Destroy ();
1053 break;
1054 case ExcludeType.MailFolder:
1055 MailFolderDialog mf_dialog = new MailFolderDialog (add_exclude_dialog);
1056 mf_dialog.ExcludeItemAddedEvent += new ExcludeItemAddedHandler (OnExcludeItemAdded);
1057 break;
1061 private void OnRadioGroupChanged (object o, EventArgs args)
1063 value_entry.Text = "";
1065 switch (Type) {
1066 case ExcludeType.Path:
1067 browse_button.Sensitive = true;
1068 value_name_label.TextWithMnemonic = Catalog.GetString ("P_ath:");
1069 value_entry.IsEditable = true;
1070 break;
1071 case ExcludeType.MailFolder:
1072 browse_button.Sensitive = true;
1073 value_name_label.TextWithMnemonic = Catalog.GetString ("M_ail folder:");
1074 value_entry.IsEditable = false;
1075 break;
1076 case ExcludeType.Pattern:
1077 browse_button.Sensitive = false;
1078 value_name_label.TextWithMnemonic = Catalog.GetString ("P_attern:");
1079 value_entry.IsEditable = true;
1080 break;
1084 private void OnExcludeItemAdded (ExcludeItem item)
1086 value = item.Value;
1087 value_entry.Text = MailFolder.GetNameForPath (item.Value);
1090 private void OnDialogResponse (object o, ResponseArgs args)
1092 if (((ResponseType)args.ResponseId) == ResponseType.Ok) {
1093 ExcludeItem exclude_item = new ExcludeItem (Type, Value);
1095 switch (Type) {
1096 case ExcludeType.Path:
1097 if (!Directory.Exists (Value)) {
1098 HigMessageDialog.RunHigMessageDialog(add_exclude_dialog,
1099 DialogFlags.Modal,
1100 MessageType.Error,
1101 ButtonsType.Ok,
1102 Catalog.GetString ("Error adding path"),
1103 Catalog.GetString ("The specified path could not be found and therefore it could not be added to the list of resources excluded for indexing."));
1104 return;
1106 break;
1109 if (ExcludeItemAddedEvent != null)
1110 ExcludeItemAddedEvent (exclude_item);
1112 add_exclude_dialog.Destroy ();
1115 public void Destroy ()
1117 add_exclude_dialog.Destroy ();
1121 #if false
1122 ////////////////////////////////////////////////////////////////
1123 // Display view
1125 class DisplayView : TreeView
1127 public DisplayView ()
1129 ListStore store = new ListStore (typeof(bool),typeof(string));
1131 this.Model = store;
1133 AppendColumn (Catalog.GetString ("Show"), new CellRendererToggle (), "active", 0);
1134 AppendColumn (Catalog.GetString ("Name"), new CellRendererText (), "text", 1);
1137 #endif