From b7bea171b53c4a1daed3c387c4b37b605cb1f2a6 Mon Sep 17 00:00:00 2001 From: Shuming Chan Date: Sat, 17 Jun 2017 21:40:40 -0400 Subject: [PATCH] Focuses on default mail client when it is already open. closes #8 --- GmailMessageTray.js | 27 +++++-------------- MailClientFocuser.js | 64 ++++++++++++++++++++++++++++++++++++++++++++ extension.js | 2 +- metadata.json | 2 +- 4 files changed, 72 insertions(+), 23 deletions(-) create mode 100644 MailClientFocuser.js diff --git a/GmailMessageTray.js b/GmailMessageTray.js index 6be22fe..9208ae3 100644 --- a/GmailMessageTray.js +++ b/GmailMessageTray.js @@ -26,11 +26,13 @@ const Main = imports.ui.main; const Util = imports.misc.util; const Lang = imports.lang; const GmailNotification = Me.imports.GmailNotification.GmailNotification; +const MailClientFocuser = new Me.imports.MailClientFocuser.MailClientFocuser(); const Source = imports.ui.messageTray.Source; const console = Me.imports.console.console; const Gettext = imports.gettext.domain('gmail_notify'); const _ = Gettext.gettext; const GmailConf = Me.imports.GmailConf; +const Gio = imports.gi.Gio; const EXTENSION_NAME = "Gmail Message Tray"; const DIALOG_ERROR = 'dialog-error'; @@ -157,7 +159,7 @@ const GmailMessageTray = new Lang.Class({ }, _displayUnreadMessages: function (content) { let newMessages = 0; - const messagesShown = Array.from(this.config.getMessagesShown()); + const messagesShown = this.config.getMessagesShown(); for (let msg of content) { const msgHash = this._simplehash(msg.link); const unseen = messagesShown.filter(h => h === msgHash).length === 0; @@ -194,32 +196,15 @@ const GmailMessageTray = new Lang.Class({ if (link === '' || link === undefined) { link = 'https://www.gmail.com'; } - try { - Util.trySpawnCommandLine("xdg-open " + link); - } catch (err) { - this._showXdgError(err); - } + const defaultBrowser = Gio.app_info_get_default_for_uri_scheme("http").get_executable(); + Util.trySpawnCommandLine(defaultBrowser + " " + link); }, _openEmail: function (link) { if (this.config.getReader() === 0) { this._openBrowser(link); } else { - try { - Util.trySpawnCommandLine("xdg-email"); - } catch (err) { - this._showXdgError(err); - } + MailClientFocuser.open(); } - }, - _showXdgError: function (err) { - console.error(err); - const content = { - from: _('Please install xdg-utils'), - date: new Date(), - subject: EXTENSION_NAME - }; - this._createNotification(content, DIALOG_ERROR, true, true, () => { - }); } }); diff --git a/MailClientFocuser.js b/MailClientFocuser.js new file mode 100644 index 0000000..7e00540 --- /dev/null +++ b/MailClientFocuser.js @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2017 Gmail Message Tray contributors + * + * Gmail Message Tray Extension 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. + * + * Gmail Message Tray Extension 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 Gnome Documents; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: + * Shuming Chan + * + */ +"use strict"; +const Lang = imports.lang; +const screen = global.screen; +const Meta = imports.gi.Meta; +const Main = imports.ui.main; +const Gio = imports.gi.Gio; +const Util = imports.misc.util; + +const MailClientFocuser = new Lang.Class({ + Name: 'MailClientFocuser', + _init: function () { + }, + open: function () { + const defaultMailClient = Gio.app_info_get_default_for_uri_scheme("mailto").get_executable(); + if(!this._trySwitchToExistingMailClient(defaultMailClient)){ + this._openNewMailClient(defaultMailClient); + } + }, + _openNewMailClient: function (mailClient) { + Util.trySpawnCommandLine(mailClient); + }, + _trySwitchToExistingMailClient: function (mailClient) { + for (let i = 0; i < screen.n_workspaces; i++) { + const workspace = screen.get_workspace_by_index(i); + if (this._trySwitchToProgram(workspace, mailClient)) { + return true; + } + } + return false; + }, + _trySwitchToProgram: function(workspace, program){ + const display = screen.get_display(); + const windows = display.get_tab_list(Meta.TabList.NORMAL, workspace); + for (let i = 0; i < windows.length; i++) { + const class_instance = windows[i].get_wm_class_instance(); + if (class_instance === program) { + Main.activateWindow(windows[i]); + return true; + } + } + return false; + } +}); diff --git a/extension.js b/extension.js index 28737e1..f06b13c 100644 --- a/extension.js +++ b/extension.js @@ -31,7 +31,7 @@ const GmailMessageTray = Me.imports.GmailMessageTray.GmailMessageTray; const Mainloop = imports.mainloop; const console = Me.imports.console.console; -const _version = "5"; +const _version = Me.metadata['version']; let extension; let Soup, sSes, Gio, Goa; diff --git a/metadata.json b/metadata.json index 43e367f..71edf66 100644 --- a/metadata.json +++ b/metadata.json @@ -7,5 +7,5 @@ ], "url": "https://github.com/shumingch/GmailMessageTray", "uuid": "GmailMessageTray@shuming0207.gmail.com", - "version": 5 + "version": 6 } \ No newline at end of file