~facundo/encuentro/trunk

« back to all changes in this revision

Viewing changes to encuentro/ui/systray.py

  • Committer: Facundo Batista
  • Date: 2013-04-16 01:58:03 UTC
  • mfrom: (151.2.7 trunk)
  • Revision ID: facundo@taniquetil.com.ar-20130416015803-btbp3sd6dn5sjyds
Merged trunk back in.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2013 Facundo Batista
 
2
#
 
3
# This program is free software: you can redistribute it and/or modify it
 
4
# under the terms of the GNU General Public License version 3, as published
 
5
# by the Free Software Foundation.
 
6
#
 
7
# This program is distributed in the hope that it will be useful, but
 
8
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
9
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
10
# PURPOSE.  See the GNU General Public License for more details.
 
11
#
 
12
# You should have received a copy of the GNU General Public License along
 
13
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
14
#
 
15
# For further info, check  https://launchpad.net/encuentro
 
16
 
 
17
"""Show an icon in the systray."""
 
18
 
 
19
import json
 
20
import logging
 
21
import os
 
22
import subprocess
 
23
 
 
24
from encuentro import platform
 
25
 
 
26
from PyQt4.QtGui import QSystemTrayIcon, QIcon, QMenu
 
27
 
 
28
logger = logging.getLogger("encuentro.systray")
 
29
 
 
30
 
 
31
def _should_fix():
 
32
    """Tell if we should fix the Unity panel systray settings.
 
33
 
 
34
    Return None if don't need, else return the current conf.
 
35
    """
 
36
    cmd = "gsettings get com.canonical.Unity.Panel systray-whitelist".split()
 
37
    try:
 
38
        out = subprocess.check_output(cmd)
 
39
    except OSError:
 
40
        # don't have gsettings, nothing to fix
 
41
        logger.debug("No gsettings, no systray conf to fix")
 
42
        return
 
43
 
 
44
    try:
 
45
        conf = map(str, json.loads(out.strip().replace("'", '"')))
 
46
    except ValueError:
 
47
        # don't understand the output, can't really fix it :/
 
48
        logger.warning("Don't understand gsettings output: %r", out)
 
49
        return
 
50
 
 
51
    logger.info("gsettings conf: %r", conf)
 
52
    if "all" in conf or "encuentro" in conf:
 
53
        # we're ok!
 
54
        return
 
55
 
 
56
    # need to fix
 
57
    return conf
 
58
 
 
59
 
 
60
def _fix_unity_systray():
 
61
    """Check settings."""
 
62
    conf = _should_fix()
 
63
    if conf is None:
 
64
        return
 
65
 
 
66
    conf.append("encuentro")
 
67
    cmd = ["gsettings", "set", "com.canonical.Unity.Panel",
 
68
           "systray-whitelist", str(conf)]
 
69
    try:
 
70
        out = subprocess.check_output(cmd)
 
71
    except OSError, err:
 
72
        logger.warning("Error trying to set the new conf: %s", err)
 
73
    else:
 
74
        logger.warning("New config set (result: %r)", out)
 
75
 
 
76
 
 
77
def show(main_window):
 
78
    """Show a system tray icon with a small icon."""
 
79
    _fix_unity_systray()
 
80
    icon = QIcon(os.path.join(platform.BASEDIR, "encuentro",
 
81
                              "logos", "icon-192.png"))
 
82
    sti = QSystemTrayIcon(icon, main_window)
 
83
    if not sti.isSystemTrayAvailable():
 
84
        logger.warning("System tray not available.")
 
85
        return
 
86
 
 
87
    def showhide(_):
 
88
        """Show or hide the main window."""
 
89
        if main_window.isVisible():
 
90
            main_window.hide()
 
91
        else:
 
92
            main_window.show()
 
93
 
 
94
    _menu = QMenu(main_window)
 
95
    _act = _menu.addAction("Mostrar/Ocultar")
 
96
    _act.triggered.connect(showhide)
 
97
    _act = _menu.addAction("Acerca de")
 
98
    _act.triggered.connect(main_window.open_about_dialog)
 
99
    _act = _menu.addAction("Salir")
 
100
    _act.triggered.connect(main_window.on_close)
 
101
    sti.setContextMenu(_menu)
 
102
    sti.show()