Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better Application shortcuts #233

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions po/POTFILES
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ src/Shortcuts/Shortcut.vala
src/Shortcuts/Settings.vala
src/Shortcuts/List.vala
src/Shortcuts/CustomShortcutSettings.vala
src/Shortcuts/ApplicationShortcutSettings.vala
src/Layout/XkbModifier.vala
src/Layout/Settings.vala
src/Layout/Handler.vala
Expand Down
165 changes: 165 additions & 0 deletions src/Shortcuts/ApplicationShortcutSettings.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
* Copyright (c) 2017 elementary, LLC. (https://elementary.io)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/

class Pantheon.Keyboard.Shortcuts.ApplicationShortcutSettings : Object {

const string SCHEMA_DEFAULT = "org.pantheon.desktop.gala.keybindings.applications";
const string SCHEMA_CUSTOM = "org.pantheon.desktop.gala.keybindings.applications.custom";
const string KEY_TEMPLATE = "applications-custom%d";
const string KEY_DESKTOP_IDS = "desktop-ids";
const int MAX_SHORTCUTS = 10;
static HashTable<string, string> keybindings_to_types;
static GLib.Settings settings_custom;
static GLib.Settings settings_default;

public struct CustomShortcut {
string name;
string desktop_id;
string shortcut;
string key;
}

public static void init () {
keybindings_to_types = new HashTable<string, string> (str_hash, str_equal);
keybindings_to_types.insert ("applications-webbrowser", "x-scheme-handler/http");
keybindings_to_types.insert ("applications-emailclient", "x-scheme-handler/mailto");
keybindings_to_types.insert ("applications-calendar", "text/calendar");
keybindings_to_types.insert ("applications-videoplayer", "video/x-ogm+ogg");
keybindings_to_types.insert ("applications-musicplayer", "audio/x-vorbis+ogg");
keybindings_to_types.insert ("applications-imageviewer", "image/jpeg");
keybindings_to_types.insert ("applications-texteditor", "text/plain");
keybindings_to_types.insert ("applications-filebrowser", "inode/directory");
keybindings_to_types.insert ("applications-terminal", "");

var schema_source = GLib.SettingsSchemaSource.get_default ();

var schema_default = schema_source.lookup (SCHEMA_DEFAULT, true);

if (schema_default == null) {
warning ("Schema \"%s\" is not installed on your system.", SCHEMA_DEFAULT);
return;
}

settings_default = new GLib.Settings.full (schema_default, null, null);

var schema_custom = schema_source.lookup (SCHEMA_CUSTOM, true);

if (schema_custom == null) {
warning ("Schema \"%s\" is not installed on your system.", SCHEMA_CUSTOM);
return;
}

settings_custom = new GLib.Settings.full (schema_custom, null, null);
}

public static GLib.List <CustomShortcut?> list_custom_shortcuts () {
var desktop_ids = settings_custom.get_strv (KEY_DESKTOP_IDS);
var l = new GLib.List <CustomShortcut?> ();
for (int i = 0; i < MAX_SHORTCUTS; i++) {
var desktop_id = desktop_ids [i];
if (desktop_id != "") {
var key = KEY_TEMPLATE.printf (i);
l.append ({
(new DesktopAppInfo (desktop_id)).get_name (),
desktop_id,
settings_custom.get_strv (key) [0],
key
});
}
}

return l;
}

public static GLib.List <CustomShortcut?> list_default_shortcuts () {
GLib.List <CustomShortcut?> l = null;
var keys = list.launchers_group.keys;
var actions = list.launchers_group.actions;

for (var i=0; i < keys.length; i++) {
var key = keys [i];
var action = actions [i];
var type = keybindings_to_types.get (key);
string desktop_id;

if (key == "applications-terminal") { // can't set default application for terminal
desktop_id = "io.elementary.terminal.desktop";
} else {
desktop_id = AppInfo.get_default_for_type (type, false).get_id ();
}

l.append ({
action,
desktop_id,
settings_default.get_strv (key) [0],
key
});
}

return l;
}

public static string? create_shortcut (AppInfo info) {
var desktop_ids = settings_custom.get_strv (KEY_DESKTOP_IDS);

for (int i = 0; i < MAX_SHORTCUTS; i++) {
if (desktop_ids [i] == "") {
desktop_ids [i] = info.get_id ();
settings_custom.set_strv (KEY_DESKTOP_IDS, desktop_ids);
return KEY_TEMPLATE.printf (i);
}
}

return (string) null;
}

public static void remove_shortcut (string key) {
var index = int.parse(key.substring (-1));
var desktop_ids = settings_custom.get_strv (KEY_DESKTOP_IDS);
desktop_ids [index] = "";
settings_custom.set_strv (KEY_DESKTOP_IDS, desktop_ids);
settings_custom.set_strv (key, {""});
}

public static bool edit_shortcut (string key, Shortcut shortcut) {
var custom = key.slice (0, -1) == KEY_TEMPLATE.slice (0, -2);
var settings = custom ? settings_custom : settings_default;
settings.set_strv (key, {shortcut.to_gsettings ()});
return true;
}

public static bool shortcut_conflicts (Shortcut new_shortcut, out string name, out string key) {
var shortcuts = list_default_shortcuts ();
shortcuts.concat (list_custom_shortcuts ());

name = "";
key = "";

foreach (var sc in shortcuts) {
var shortcut = new Shortcut.parse (sc.shortcut);
if (shortcut.is_equal (new_shortcut)) {
name = sc.name;
key = sc.key;
return true;
}
}

return false;
}
}
1 change: 1 addition & 0 deletions src/Shortcuts/CustomShortcutSettings.vala
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class Pantheon.Keyboard.Shortcuts.CustomShortcutSettings : Object {
}

public static string? create_shortcut () requires (available) {
debug ("create shortcut!");
for (int i = 0; i < MAX_SHORTCUTS; i++) {
var new_relocatable_schema = get_relocatable_schema_path (i);

Expand Down
14 changes: 9 additions & 5 deletions src/Shortcuts/List.vala
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,15 @@ namespace Pantheon.Keyboard.Shortcuts {
launchers_group = {};
launchers_group.icon_name = "preferences-desktop-applications";
launchers_group.label = _("Applications");
add_action (ref launchers_group, Schema.MEDIA, _("Email"), "email");
add_action (ref launchers_group, Schema.MEDIA, _("Home Folder"), "home");
add_action (ref launchers_group, Schema.MEDIA, _("Music"), "media");
add_action (ref launchers_group, Schema.MEDIA, _("Terminal"), "terminal");
add_action (ref launchers_group, Schema.MEDIA, _("Internet Browser"), "www");
add_action (ref launchers_group, Schema.APPS, _("Web Browser"), "applications-webbrowser");
add_action (ref launchers_group, Schema.APPS, _("Email Client"), "applications-emailclient");
add_action (ref launchers_group, Schema.APPS, _("Calendar"), "applications-calendar");
add_action (ref launchers_group, Schema.APPS, _("Video Player"), "applications-videoplayer");
add_action (ref launchers_group, Schema.APPS, _("Music Player"), "applications-musicplayer");
add_action (ref launchers_group, Schema.APPS, _("Image Viewer"), "applications-imageviewer");
add_action (ref launchers_group, Schema.APPS, _("Text Editor"), "applications-texteditor");
add_action (ref launchers_group, Schema.APPS, _("File Browser"), "applications-filebrowser");
add_action (ref launchers_group, Schema.APPS, _("Terminal"), "applications-terminal");

media_group = {};
media_group.icon_name = "applications-multimedia";
Expand Down
3 changes: 2 additions & 1 deletion src/Shortcuts/Settings.vala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

namespace Pantheon.Keyboard.Shortcuts
{
private enum Schema { WM, MUTTER, GALA, MEDIA, COUNT }
private enum Schema { WM, MUTTER, GALA, APPS, MEDIA, COUNT }

// helper class for gsettings
// note that media key are stored as strings, all others as string vectors
Expand All @@ -34,6 +34,7 @@ namespace Pantheon.Keyboard.Shortcuts
"org.gnome.desktop.wm.keybindings",
"org.gnome.mutter.keybindings",
"org.pantheon.desktop.gala.keybindings",
"org.pantheon.desktop.gala.keybindings.applications",
"org.gnome.settings-daemon.plugins.media-keys"
};

Expand Down
9 changes: 7 additions & 2 deletions src/Views/Shortcuts.vala
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,17 @@ namespace Pantheon.Keyboard.Shortcuts {

construct {
CustomShortcutSettings.init ();
ApplicationShortcutSettings.init ();

list = new List ();
settings = new Shortcuts.Settings ();

for (int id = 0; id < SectionID.CUSTOM; id++) {
trees += new Tree ((SectionID) id);
if (id == SectionID.APPS) {
trees += new ApplicationTree ();
} else {
trees += new Tree ((SectionID) id);
}
}

if (CustomShortcutSettings.available) {
Expand All @@ -73,7 +78,7 @@ namespace Pantheon.Keyboard.Shortcuts {
section_switcher.add_section (list.system_group);
section_switcher.add_section (list.custom_group);

section_switcher.set_selected (0);
section_switcher.set_selected (3);

var shortcut_display = new ShortcutDisplay (trees);

Expand Down
Loading