| 1 |
/* |
| 2 |
* Copyright (c) 2017-2019 José Amuedo (https://github.com/spheras) |
| 3 |
* |
| 4 |
* This program is free software: you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation, either version 3 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
|
| 18 |
class Pantheon.Keyboard.Shortcuts.CustomShortcutSettings : Object { |
| 19 |
|
| 20 |
const string SCHEMA = "org.gnome.settings-daemon.plugins.media-keys"; |
| 21 |
const string KEY = "custom-keybinding"; |
| 22 |
|
| 23 |
const string RELOCATABLE_SCHEMA_PATH_TEMLPATE = "/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom%d/"; |
| 24 |
|
| 25 |
const int MAX_SHORTCUTS = 100; |
| 26 |
|
| 27 |
static GLib.Settings settings; |
| 28 |
|
| 29 |
public static bool available = false; |
| 30 |
|
| 31 |
public struct CustomShortcut { |
| 32 |
string shortcut; |
| 33 |
string command; |
| 34 |
string relocatable_schema; |
| 35 |
} |
| 36 |
|
| 37 |
public static void init () { |
| 38 |
var schema_source = GLib.SettingsSchemaSource.get_default (); |
| 39 |
|
| 40 |
var schema = schema_source.lookup (SCHEMA, true); |
| 41 |
|
| 42 |
if (schema == null) { |
| 43 |
warning ("Schema \"%s\" is not installed on your system.", SCHEMA); |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
settings = new GLib.Settings.full (schema, null, null); |
| 48 |
available = true; |
| 49 |
} |
| 50 |
|
| 51 |
static string[] get_relocatable_schemas () { |
| 52 |
return settings.get_strv (KEY + "s"); |
| 53 |
} |
| 54 |
|
| 55 |
static string get_relocatable_schema_path (int i) { |
| 56 |
return RELOCATABLE_SCHEMA_PATH_TEMLPATE.printf (i); |
| 57 |
} |
| 58 |
|
| 59 |
static GLib.Settings ? get_relocatable_schema_settings (string relocatable_schema) { |
| 60 |
return new GLib.Settings.with_path (SCHEMA + "." + KEY, relocatable_schema); |
| 61 |
} |
| 62 |
|
| 63 |
public static string ? create_shortcut () requires (available) { |
| 64 |
for (int i = 0; i < MAX_SHORTCUTS; i++) { |
| 65 |
var new_relocatable_schema = get_relocatable_schema_path (i); |
| 66 |
|
| 67 |
if (relocatable_schema_is_used (new_relocatable_schema) == false) { |
| 68 |
reset_relocatable_schema (new_relocatable_schema); |
| 69 |
add_relocatable_schema (new_relocatable_schema); |
| 70 |
return new_relocatable_schema; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
return (string) null; |
| 75 |
} |
| 76 |
|
| 77 |
static bool relocatable_schema_is_used (string new_relocatable_schema) { |
| 78 |
var relocatable_schemas = get_relocatable_schemas (); |
| 79 |
|
| 80 |
foreach (var relocatable_schema in relocatable_schemas) |
| 81 |
if (relocatable_schema == new_relocatable_schema) |
| 82 |
return true; |
| 83 |
|
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
static void add_relocatable_schema (string new_relocatable_schema) { |
| 88 |
var relocatable_schemas = get_relocatable_schemas (); |
| 89 |
relocatable_schemas += new_relocatable_schema; |
| 90 |
settings.set_strv (KEY + "s", relocatable_schemas); |
| 91 |
apply_settings (settings); |
| 92 |
} |
| 93 |
|
| 94 |
static void reset_relocatable_schema (string relocatable_schema) { |
| 95 |
var relocatable_settings = get_relocatable_schema_settings (relocatable_schema); |
| 96 |
relocatable_settings.reset ("name"); |
| 97 |
relocatable_settings.reset ("command"); |
| 98 |
relocatable_settings.reset ("binding"); |
| 99 |
apply_settings (relocatable_settings); |
| 100 |
} |
| 101 |
|
| 102 |
/* NEVER USED |
| 103 |
public static void remove_shortcut (string relocatable_schema) |
| 104 |
requires (available) { |
| 105 |
|
| 106 |
string []relocatable_schemas = {}; |
| 107 |
|
| 108 |
foreach (var schema in get_relocatable_schemas ()) |
| 109 |
if (schema != relocatable_schema) |
| 110 |
relocatable_schemas += schema; |
| 111 |
|
| 112 |
reset_relocatable_schema (relocatable_schema); |
| 113 |
settings.set_strv (KEY + "s", relocatable_schemas); |
| 114 |
apply_settings (settings); |
| 115 |
} |
| 116 |
*/ |
| 117 |
|
| 118 |
public static bool edit_shortcut (string relocatable_schema, string shortcut) |
| 119 |
requires (available) { |
| 120 |
|
| 121 |
var relocatable_settings = get_relocatable_schema_settings (relocatable_schema); |
| 122 |
relocatable_settings.set_string ("binding", shortcut); |
| 123 |
apply_settings (relocatable_settings); |
| 124 |
return true; |
| 125 |
} |
| 126 |
|
| 127 |
public static bool edit_command (string relocatable_schema, string command) |
| 128 |
requires (available) { |
| 129 |
|
| 130 |
var relocatable_settings = get_relocatable_schema_settings (relocatable_schema); |
| 131 |
relocatable_settings.set_string ("command", command); |
| 132 |
relocatable_settings.set_string ("name", command); |
| 133 |
apply_settings (relocatable_settings); |
| 134 |
return true; |
| 135 |
} |
| 136 |
|
| 137 |
public static GLib.List <CustomShortcut ? > list_custom_shortcuts () |
| 138 |
requires (available) { |
| 139 |
|
| 140 |
var list = new GLib.List <CustomShortcut ? > (); |
| 141 |
foreach (var relocatable_schema in get_relocatable_schemas ()) |
| 142 |
list.append (create_custom_shortcut_object (relocatable_schema)); |
| 143 |
return list; |
| 144 |
} |
| 145 |
|
| 146 |
static CustomShortcut ? create_custom_shortcut_object (string relocatable_schema) { |
| 147 |
var relocatable_settings = get_relocatable_schema_settings (relocatable_schema); |
| 148 |
|
| 149 |
return { |
| 150 |
relocatable_settings.get_string ("binding"), |
| 151 |
relocatable_settings.get_string ("command"), |
| 152 |
relocatable_schema |
| 153 |
}; |
| 154 |
} |
| 155 |
|
| 156 |
public static bool shortcut_conflicts (Shortcut new_shortcut, out string command, |
| 157 |
out string relocatable_schema) { |
| 158 |
var custom_shortcuts = list_custom_shortcuts (); |
| 159 |
command = ""; |
| 160 |
relocatable_schema = ""; |
| 161 |
|
| 162 |
foreach (var custom_shortcut in custom_shortcuts) { |
| 163 |
// debug("checking:%s",custom_shortcut.shortcut); |
| 164 |
var shortcut = new Shortcut.parse (custom_shortcut.shortcut); |
| 165 |
if (shortcut.is_equal (new_shortcut)) { |
| 166 |
command = custom_shortcut.command; |
| 167 |
relocatable_schema = custom_shortcut.relocatable_schema; |
| 168 |
return true; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
return false; |
| 173 |
} |
| 174 |
|
| 175 |
private static void apply_settings (GLib.Settings asettings) { |
| 176 |
asettings.apply (); |
| 177 |
GLib.Settings.sync (); |
| 178 |
} |
| 179 |
|
| 180 |
} |