~vcs-imports/beagle/trunk

« back to all changes in this revision

Viewing changes to tools/Settings.cs

  • Committer: dbera
  • Date: 2008-07-08 23:52:06 UTC
  • Revision ID: vcs-imports@canonical.com-20080708235206-1axm3o8wmkjxawcq
Add a python script (from CCSM) to grab shortcut keys from user input. Use the script in beagle-settings to get the shortcut key for beagle-search. Store the entire binding string in config instead of separately storing ctrl, alt and the key. Note that CCSM is GPLv2+ so we are including the script as GPLv3. It is possible to create a C# program to do what the script does but I am not taking the risk yet.

Show diffs side-by-side

added added

removed removed

Lines of Context:
74
74
        [Widget] CheckButton battery_toggle;
75
75
        [Widget] CheckButton screensaver_toggle;
76
76
        [Widget] CheckButton auto_search_toggle;
77
 
        [Widget] CheckButton press_ctrl_toggle;
78
 
        [Widget] CheckButton press_alt_toggle;
79
 
 
80
 
        [Widget] Entry show_search_window_entry;
 
77
        [Widget] Label shortcut_label;
 
78
        [Widget] Button shortcut_button;
 
79
        string binding;
81
80
 
82
81
        [Widget] CheckButton index_home_toggle;
83
82
        [Widget] Button remove_include_button;
162
161
                networking_password_box.Sensitive = false;
163
162
                require_password_toggle.Sensitive = false;
164
163
 
 
164
                // Keybinding button
 
165
 
 
166
                shortcut_button.Clicked += new EventHandler (OnKeybindingClicked);
 
167
 
165
168
                LoadConfiguration ();
166
169
 
167
170
                Conf.Subscribe (Conf.Names.FilesQueryableConfig, new Conf.ConfigUpdateHandler (OnConfigurationChanged));
216
219
 
217
220
                autostart_toggle.Active = IsAutostartEnabled ();
218
221
 
219
 
                bool binding_ctrl = bs_config.GetOption (Conf.Names.KeyBinding_Ctrl, false);
220
 
                bool binding_alt = bs_config.GetOption (Conf.Names.KeyBinding_Alt, false);
221
 
                string binding_key = bs_config.GetOption (Conf.Names.KeyBinding_Key, "F12");
222
 
                KeyBinding show_binding = new KeyBinding (binding_key, binding_ctrl, binding_alt);
223
 
                press_ctrl_toggle.Active = show_binding.Ctrl;
224
 
                press_alt_toggle.Active = show_binding.Alt;
225
 
                show_search_window_entry.Text = show_binding.Key;
 
222
                binding = bs_config.GetOption ("KeyBinding", null);
 
223
                if (String.IsNullOrEmpty (binding)) {
 
224
                        // Move old preference value to new
 
225
                        bool binding_ctrl = bs_config.GetOption (Conf.Names.KeyBinding_Ctrl, false);
 
226
                        bool binding_alt = bs_config.GetOption (Conf.Names.KeyBinding_Alt, false);
 
227
                        string binding_key = bs_config.GetOption (Conf.Names.KeyBinding_Key, "F12");
 
228
                        KeyBinding show_binding = new KeyBinding (binding_key, binding_ctrl, binding_alt);
 
229
 
 
230
                        binding = show_binding.ToString ();
 
231
                }
 
232
 
 
233
                shortcut_label.Text = String.Format (Catalog.GetString ("Display the search window by pressing {0}"), binding);
226
234
 
227
235
                if (fsq_config.GetOption (Conf.Names.IndexHomeDir, true))
228
236
                        index_home_toggle.Active = true;
282
290
                Config bs_config = Conf.Get (Conf.Names.BeagleSearchConfig);
283
291
 
284
292
                daemon_config.SetOption (Conf.Names.AllowRoot, allow_root_toggle.Active);
285
 
                bs_config.SetOption (Conf.Names.BeagleSearchAutoSearch,auto_search_toggle.Active);
286
293
                daemon_config.SetOption (Conf.Names.IndexOnBattery,battery_toggle.Active);
287
294
                daemon_config.SetOption (Conf.Names.IndexFasterOnScreensaver, screensaver_toggle.Active);
288
295
 
289
 
                bs_config.SetOption (Conf.Names.KeyBinding_Key, show_search_window_entry.Text);
290
 
                bs_config.SetOption (Conf.Names.KeyBinding_Ctrl, press_ctrl_toggle.Active);
291
 
                bs_config.SetOption (Conf.Names.KeyBinding_Alt, press_alt_toggle.Active);
 
296
                bs_config.SetOption (Conf.Names.BeagleSearchAutoSearch,auto_search_toggle.Active);
 
297
                bs_config.SetOption ("KeyBinding", binding);
292
298
 
293
299
                fsq_config.SetOption (Conf.Names.IndexHomeDir, index_home_toggle.Active);
294
300
 
690
696
                //networking_password_box.Sensitive = require_password_toggle.Active;
691
697
        }
692
698
 
 
699
        private void OnKeybindingClicked (object o, EventArgs args)
 
700
        {
 
701
                try {
 
702
                        string new_binding = GetBindingFromKeygrabber ();
 
703
                        if (! String.IsNullOrEmpty (new_binding))
 
704
                                binding = new_binding;
 
705
                } catch (Exception e) {
 
706
                        Console.WriteLine ("Could not run python keygrabber: {0}", e.Message);
 
707
                        Console.WriteLine ("Showing old keybinding widget");
 
708
                        binding = GetBindingFromUserInput ();
 
709
                }
 
710
 
 
711
                shortcut_label.Text = String.Format (Catalog.GetString ("Display the search window by pressing {0}"), binding);
 
712
        }
 
713
 
 
714
        private static string GetBindingFromKeygrabber ()
 
715
        {
 
716
                SafeProcess pc = new SafeProcess ();
 
717
 
 
718
                string keygrabber_file = Path.Combine (ExternalStringsHack.PkgLibDir, "keygrabber.py");
 
719
                if (! File.Exists (keygrabber_file))
 
720
                        throw new Exception ("keygrabber.py not found");
 
721
 
 
722
                pc.Arguments = new string[] {"python", keygrabber_file};
 
723
                pc.RedirectStandardError = false;
 
724
                pc.RedirectStandardOutput = true;
 
725
 
 
726
                pc.Start ();
 
727
                string output;
 
728
                using (StreamReader pout = new StreamReader (pc.StandardOutput))
 
729
                        output = pout.ReadLine ();
 
730
                pc.Close ();
 
731
 
 
732
                Console.WriteLine ("New binding '{0}'", output);
 
733
                return output;
 
734
        }
 
735
 
 
736
        private string GetBindingFromUserInput ()
 
737
        {
 
738
                return UserShortcutDialog.GetUserShortcut (settings_dialog, binding);
 
739
        }
 
740
 
693
741
        ////////////////////////////////////////////////////////////////
694
742
        // IncludeView 
695
743
 
1344
1392
                }
1345
1393
        }
1346
1394
 
 
1395
        ////////////////////////////////////////////////////////////////
 
1396
        // ShortcutInput Dialog
 
1397
        // Simple dialog that is shown in case the python keygrabber script
 
1398
        // cannot be run.
 
1399
        // This deliberately does not try to cover all cases. We assume
 
1400
        // everyone has python installed.
 
1401
 
 
1402
        public class UserShortcutDialog : Dialog
 
1403
        {
 
1404
                CheckButton ctrl_button, alt_button;
 
1405
                Entry entry;
 
1406
                string binding;
 
1407
 
 
1408
                public UserShortcutDialog (Gtk.Window parent, string binding_string) : base (null, parent, DialogFlags.DestroyWithParent)
 
1409
                {
 
1410
                        Title = "KeyGrabber";
 
1411
                        Modal = true;
 
1412
                        HasSeparator = false;
 
1413
 
 
1414
                        AddButton ("Ok", ResponseType.Ok);
 
1415
                        AddButton ("Cancel", ResponseType.Cancel);
 
1416
 
 
1417
                        binding = binding_string;
 
1418
 
 
1419
                        ctrl_button = new CheckButton ("Ctrl");
 
1420
                        int i = binding_string.IndexOf ("<ctrl>", StringComparison.InvariantCultureIgnoreCase);
 
1421
                        if (i != -1) {
 
1422
                                ctrl_button.Active = true;
 
1423
                                binding_string = binding_string.Remove (i, 6);
 
1424
                        }
 
1425
 
 
1426
                        alt_button = new CheckButton ("Alt");
 
1427
                        i = binding_string.IndexOf ("<alt>", StringComparison.InvariantCultureIgnoreCase);
 
1428
                        if (i != -1) {
 
1429
                                alt_button.Active = true;
 
1430
                                binding_string = binding_string.Remove (i, 5);
 
1431
                        }
 
1432
 
 
1433
                        entry = new Entry ();
 
1434
                        entry.Text = binding_string;
 
1435
 
 
1436
                        HBox box = new HBox (false, 0);
 
1437
                        box.PackEnd (ctrl_button, true, false, 0);
 
1438
                        ctrl_button.Show ();
 
1439
                        box.PackEnd (alt_button, true, false, 0);
 
1440
                        alt_button.Show ();
 
1441
                        box.PackEnd (entry, true, false, 0);
 
1442
                        entry.Show ();
 
1443
 
 
1444
                        VBox.PackStart (box, true, false, 0);
 
1445
                        box.Show ();
 
1446
                }
 
1447
 
 
1448
                public static string GetUserShortcut (Gtk.Window parent, string binding)
 
1449
                {
 
1450
                        UserShortcutDialog dialog = new UserShortcutDialog (parent, binding);
 
1451
                        dialog.Response += new ResponseHandler (OnResponse);
 
1452
                        dialog.Run ();
 
1453
                        dialog.Destroy ();
 
1454
 
 
1455
                        Console.WriteLine ("new binding = '{0}'", dialog.binding);
 
1456
                        return dialog.binding;
 
1457
                }
 
1458
 
 
1459
                private static void OnResponse (object obj, ResponseArgs args)
 
1460
                {
 
1461
                        UserShortcutDialog dialog = (UserShortcutDialog) obj;
 
1462
                        if (args.ResponseId != ResponseType.Ok)
 
1463
                                return;
 
1464
 
 
1465
                        bool ctrl = dialog.ctrl_button.Active;
 
1466
                        bool alt = dialog.alt_button.Active;
 
1467
                        string key = dialog.entry.Text;
 
1468
                        dialog.binding = (new KeyBinding (key, ctrl, alt)).ToString ();
 
1469
                }
 
1470
        }
 
1471
 
1347
1472
        ////////////////////////////////////////////////////////////////
1348
1473
        // AddHostDialog
1349
1474