~umang/indicator-stickynotes/trunk

24 by Umang Varma
Added .desktop and setup.py files.
1
#!/usr/bin/python3
2
# 
67 by Umang Varma
Release 0.3.2
3
# Copyright © 2012-2013 Umang Varma <umang.me@gmail.com>
24 by Umang Varma
Added .desktop and setup.py files.
4
# 
5
# This file is part of indicator-stickynotes.
6
# 
7
# indicator-stickynotes is free software: you can redistribute it and/or
8
# modify it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or (at your
10
# option) any later version.
11
# 
12
# indicator-stickynotes is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15
# more details.
16
# 
17
# You should have received a copy of the GNU General Public License along with
18
# indicator-stickynotes.  If not, see <http://www.gnu.org/licenses/>.
19
20
from stickynotes.backend import Note, NoteSet
47 by Umang Varma
Added settings dialog, which controls bgcolor
21
from stickynotes.gui import StickyNote, show_about_dialog, \
22
    SettingsDialog, load_global_css
62 by Umang Varma
Added -d parameter for development data file
23
import stickynotes.info
36 by Umang Varma
Start using python's locale for l10n
24
from stickynotes.info import MO_DIR, LOCALE_DOMAIN
32 by Umang Varma
Save more often so changes aren't lost
25
24 by Umang Varma
Added .desktop and setup.py files.
26
from gi.repository import Gtk, Gdk
27
from gi.repository import AppIndicator3 as appindicator
32 by Umang Varma
Save more often so changes aren't lost
28
30 by Umang Varma
Added ubuntu-mono themed icons.
29
import os.path
36 by Umang Varma
Start using python's locale for l10n
30
import locale
62 by Umang Varma
Added -d parameter for development data file
31
import argparse
36 by Umang Varma
Start using python's locale for l10n
32
from locale import gettext as _
32 by Umang Varma
Save more often so changes aren't lost
33
from functools import wraps
34
35
def save_required(f):
36
    """Wrapper for functions that require a save after execution"""
37
    @wraps(f)
38
    def _wrapper(self, *args, **kwargs):
39
        ret = f(self, *args, **kwargs)
40
        self.save()
41
        return ret
42
    return _wrapper
24 by Umang Varma
Added .desktop and setup.py files.
43
44
class IndicatorStickyNotes:
62 by Umang Varma
Added -d parameter for development data file
45
    def __init__(self, args = None):
46
        self.args = args
47
        # use development data file if requested
48
        isdev = args and args.d
49
        data_file = stickynotes.info.DEBUG_SETTINGS_FILE if isdev else \
50
                stickynotes.info.SETTINGS_FILE
24 by Umang Varma
Added .desktop and setup.py files.
51
        # Initialize NoteSet
62 by Umang Varma
Added -d parameter for development data file
52
        self.nset = NoteSet(StickyNote, data_file)
24 by Umang Varma
Added .desktop and setup.py files.
53
        self.nset.open()
58 by Umang Varma
Remember hidden state on startup
54
        if self.nset.properties.get("all_visible", True):
55
            self.nset.showall()
24 by Umang Varma
Added .desktop and setup.py files.
56
        # Create App Indicator
57
        self.ind = appindicator.Indicator.new(
58
                "Sticky Notes", "indicator-stickynotes",
59
                appindicator.IndicatorCategory.APPLICATION_STATUS)
30 by Umang Varma
Added ubuntu-mono themed icons.
60
        # Delete/modify the following file when distributing as a package
61
        self.ind.set_icon_theme_path(os.path.abspath(os.path.join(
62
            os.path.dirname(__file__), 'Icons')))
63
        self.ind.set_icon("indicator-stickynotes")
24 by Umang Varma
Added .desktop and setup.py files.
64
        self.ind.set_status(appindicator.IndicatorStatus.ACTIVE)
36 by Umang Varma
Start using python's locale for l10n
65
        self.ind.set_title(_("Sticky Notes"))
24 by Umang Varma
Added .desktop and setup.py files.
66
        # Create Menu
67
        self.menu = Gtk.Menu()
36 by Umang Varma
Start using python's locale for l10n
68
        self.mNewNote = Gtk.MenuItem(_("New Note"))
24 by Umang Varma
Added .desktop and setup.py files.
69
        self.menu.append(self.mNewNote)
70
        self.mNewNote.connect("activate", self.new_note, None)
71
        self.mNewNote.show()
72
73
        s = Gtk.SeparatorMenuItem.new()
74
        self.menu.append(s)
75
        s.show()
76
36 by Umang Varma
Start using python's locale for l10n
77
        self.mShowAll = Gtk.MenuItem(_("Show All"))
24 by Umang Varma
Added .desktop and setup.py files.
78
        self.menu.append(self.mShowAll)
79
        self.mShowAll.connect("activate", self.showall, None)
80
        self.mShowAll.show()
81
36 by Umang Varma
Start using python's locale for l10n
82
        self.mHideAll = Gtk.MenuItem(_("Hide All"))
24 by Umang Varma
Added .desktop and setup.py files.
83
        self.menu.append(self.mHideAll)
84
        self.mHideAll.connect("activate", self.hideall, None)
85
        self.mHideAll.show()
86
87
        s = Gtk.SeparatorMenuItem.new()
88
        self.menu.append(s)
89
        s.show()
90
36 by Umang Varma
Start using python's locale for l10n
91
        self.mLockAll = Gtk.MenuItem(_("Lock All"))
24 by Umang Varma
Added .desktop and setup.py files.
92
        self.menu.append(self.mLockAll)
93
        self.mLockAll.connect("activate", self.lockall, None)
94
        self.mLockAll.show()
95
36 by Umang Varma
Start using python's locale for l10n
96
        self.mUnlockAll = Gtk.MenuItem(_("Unlock All"))
24 by Umang Varma
Added .desktop and setup.py files.
97
        self.menu.append(self.mUnlockAll)
98
        self.mUnlockAll.connect("activate", self.unlockall, None)
99
        self.mUnlockAll.show()
100
101
        s = Gtk.SeparatorMenuItem.new()
102
        self.menu.append(s)
103
        s.show()
104
41 by Umang Varma
Added about dialog
105
        self.mAbout = Gtk.MenuItem(_("About"))
106
        self.menu.append(self.mAbout)
107
        self.mAbout.connect("activate", self.show_about, None)
108
        self.mAbout.show()
109
47 by Umang Varma
Added settings dialog, which controls bgcolor
110
        self.mSettings = Gtk.MenuItem(_("Settings"))
111
        self.menu.append(self.mSettings)
112
        self.mSettings.connect("activate", self.show_settings, None)
113
        self.mSettings.show()
114
41 by Umang Varma
Added about dialog
115
        s = Gtk.SeparatorMenuItem.new()
116
        self.menu.append(s)
117
        s.show()
118
36 by Umang Varma
Start using python's locale for l10n
119
        self.mQuit = Gtk.MenuItem(_("Quit"))
24 by Umang Varma
Added .desktop and setup.py files.
120
        self.menu.append(self.mQuit)
121
        self.mQuit.connect("activate", Gtk.main_quit, None)
122
        self.mQuit.show()
123
        # Connect Indicator to menu
124
        self.ind.set_menu(self.menu)
125
63 by Umang Varma
Middle-click toggles Show/Hide All.
126
        # Define secondary action (middle click)
127
        self.connect_secondary_activate()
128
24 by Umang Varma
Added .desktop and setup.py files.
129
    def new_note(self, *args):
130
        self.nset.new()
131
132
    def showall(self, *args):
133
        self.nset.showall(*args)
63 by Umang Varma
Middle-click toggles Show/Hide All.
134
        self.connect_secondary_activate()
24 by Umang Varma
Added .desktop and setup.py files.
135
136
    def hideall(self, *args):
137
        self.nset.hideall()
63 by Umang Varma
Middle-click toggles Show/Hide All.
138
        self.connect_secondary_activate()
139
140
    def connect_secondary_activate(self):
141
        """Define action of secondary action (middle click) depending
142
        on visibility state of notes."""
143
        if self.nset.properties["all_visible"] == True:
144
            self.ind.set_secondary_activate_target(self.mHideAll)
145
        else:
146
            self.ind.set_secondary_activate_target(self.mShowAll)
147
24 by Umang Varma
Added .desktop and setup.py files.
148
32 by Umang Varma
Save more often so changes aren't lost
149
    @save_required
24 by Umang Varma
Added .desktop and setup.py files.
150
    def lockall(self, *args):
151
        for note in self.nset.notes:
152
            note.gui.set_locked_state(True)
153
        
32 by Umang Varma
Save more often so changes aren't lost
154
    @save_required
24 by Umang Varma
Added .desktop and setup.py files.
155
    def unlockall(self, *args):
156
        for note in self.nset.notes:
157
            note.gui.set_locked_state(False)
158
41 by Umang Varma
Added about dialog
159
    def show_about(self, *args):
160
        show_about_dialog()
161
47 by Umang Varma
Added settings dialog, which controls bgcolor
162
    def show_settings(self, *args):
163
        wSettings = SettingsDialog(self.nset)
164
24 by Umang Varma
Added .desktop and setup.py files.
165
    def save(self):
166
        self.nset.save()
167
168
169
def main():
36 by Umang Varma
Start using python's locale for l10n
170
    try:
171
        locale.setlocale(locale.LC_ALL, '')
172
    except:
173
        locale.setlocale(locale.LC_ALL, 'C')
174
    # If we're running from /usr, then .mo files are not in MO_DIR.
175
    if os.path.abspath(__file__)[:4] == '/usr':
176
        # Fallback to default
177
        locale_dir = None
178
    else:
39 by Umang Varma
Find locale_dir correctly
179
        locale_dir = os.path.join(os.path.dirname(__file__), MO_DIR)
36 by Umang Varma
Start using python's locale for l10n
180
    locale.bindtextdomain(LOCALE_DOMAIN, locale_dir)
181
    locale.textdomain(LOCALE_DOMAIN)
62 by Umang Varma
Added -d parameter for development data file
182
77 by Umang Varma
Updated pot file and some strings
183
    parser = argparse.ArgumentParser(description=_("Sticky Notes"))
62 by Umang Varma
Added -d parameter for development data file
184
    parser.add_argument("-d", action='store_true', help="use the development"
185
            " data file")
186
    args = parser.parse_args()
187
188
    indicator = IndicatorStickyNotes(args)
46 by Umang Varma
Notes can now change color.
189
    # Load global css for the first time.
190
    load_global_css()
24 by Umang Varma
Added .desktop and setup.py files.
191
    Gtk.main()
192
    indicator.save()
193
194
if __name__ == "__main__":
195
    main()