Skip to content

Commit

Permalink
Focuses on default mail client when it is already open. closes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
shumingch committed Jun 18, 2017
1 parent 29d3bd7 commit b7bea17
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 23 deletions.
27 changes: 6 additions & 21 deletions GmailMessageTray.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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, () => {
});
}
});

64 changes: 64 additions & 0 deletions MailClientFocuser.js
Original file line number Diff line number Diff line change
@@ -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 <shuming0207@gmail.com>
*
*/
"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;
}
});
2 changes: 1 addition & 1 deletion extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
],
"url": "https://github.com/shumingch/GmailMessageTray",
"uuid": "GmailMessageTray@shuming0207.gmail.com",
"version": 5
"version": 6
}

0 comments on commit b7bea17

Please sign in to comment.