~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
96 by Umang Varma
Add settings option to menus
52
        self.nset = NoteSet(StickyNote, data_file, self)
24 by Umang Varma
Added .desktop and setup.py files.
53
        self.nset.open()
102 by Umang Varma
Don't initialize gui just before hiding window
54
        # If all notes were visible previously, show them now
58 by Umang Varma
Remember hidden state on startup
55
        if self.nset.properties.get("all_visible", True):
56
            self.nset.showall()
24 by Umang Varma
Added .desktop and setup.py files.
57
        # Create App Indicator
58
        self.ind = appindicator.Indicator.new(
59
                "Sticky Notes", "indicator-stickynotes",
60
                appindicator.IndicatorCategory.APPLICATION_STATUS)
30 by Umang Varma
Added ubuntu-mono themed icons.
61
        # Delete/modify the following file when distributing as a package
62
        self.ind.set_icon_theme_path(os.path.abspath(os.path.join(
63
            os.path.dirname(__file__), 'Icons')))
64
        self.ind.set_icon("indicator-stickynotes")
24 by Umang Varma
Added .desktop and setup.py files.
65
        self.ind.set_status(appindicator.IndicatorStatus.ACTIVE)
36 by Umang Varma
Start using python's locale for l10n
66
        self.ind.set_title(_("Sticky Notes"))
24 by Umang Varma
Added .desktop and setup.py files.
67
        # Create Menu
68
        self.menu = Gtk.Menu()
36 by Umang Varma
Start using python's locale for l10n
69
        self.mNewNote = Gtk.MenuItem(_("New Note"))
24 by Umang Varma
Added .desktop and setup.py files.
70
        self.menu.append(self.mNewNote)
71
        self.mNewNote.connect("activate", self.new_note, None)
72
        self.mNewNote.show()
73
74
        s = Gtk.SeparatorMenuItem.new()
75
        self.menu.append(s)
76
        s.show()
77
36 by Umang Varma
Start using python's locale for l10n
78
        self.mShowAll = Gtk.MenuItem(_("Show All"))
24 by Umang Varma
Added .desktop and setup.py files.
79
        self.menu.append(self.mShowAll)
80
        self.mShowAll.connect("activate", self.showall, None)
81
        self.mShowAll.show()
82
36 by Umang Varma
Start using python's locale for l10n
83
        self.mHideAll = Gtk.MenuItem(_("Hide All"))
24 by Umang Varma
Added .desktop and setup.py files.
84
        self.menu.append(self.mHideAll)
85
        self.mHideAll.connect("activate", self.hideall, None)
86
        self.mHideAll.show()
87
88
        s = Gtk.SeparatorMenuItem.new()
89
        self.menu.append(s)
90
        s.show()
91
36 by Umang Varma
Start using python's locale for l10n
92
        self.mLockAll = Gtk.MenuItem(_("Lock All"))
24 by Umang Varma
Added .desktop and setup.py files.
93
        self.menu.append(self.mLockAll)
94
        self.mLockAll.connect("activate", self.lockall, None)
95
        self.mLockAll.show()
96
36 by Umang Varma
Start using python's locale for l10n
97
        self.mUnlockAll = Gtk.MenuItem(_("Unlock All"))
24 by Umang Varma
Added .desktop and setup.py files.
98
        self.menu.append(self.mUnlockAll)
99
        self.mUnlockAll.connect("activate", self.unlockall, None)
100
        self.mUnlockAll.show()
101
102
        s = Gtk.SeparatorMenuItem.new()
103
        self.menu.append(s)
104
        s.show()
105
41 by Umang Varma
Added about dialog
106
        self.mAbout = Gtk.MenuItem(_("About"))
107
        self.menu.append(self.mAbout)
108
        self.mAbout.connect("activate", self.show_about, None)
109
        self.mAbout.show()
110
47 by Umang Varma
Added settings dialog, which controls bgcolor
111
        self.mSettings = Gtk.MenuItem(_("Settings"))
112
        self.menu.append(self.mSettings)
113
        self.mSettings.connect("activate", self.show_settings, None)
114
        self.mSettings.show()
115
41 by Umang Varma
Added about dialog
116
        s = Gtk.SeparatorMenuItem.new()
117
        self.menu.append(s)
118
        s.show()
119
36 by Umang Varma
Start using python's locale for l10n
120
        self.mQuit = Gtk.MenuItem(_("Quit"))
24 by Umang Varma
Added .desktop and setup.py files.
121
        self.menu.append(self.mQuit)
122
        self.mQuit.connect("activate", Gtk.main_quit, None)
123
        self.mQuit.show()
124
        # Connect Indicator to menu
125
        self.ind.set_menu(self.menu)
126
63 by Umang Varma
Middle-click toggles Show/Hide All.
127
        # Define secondary action (middle click)
128
        self.connect_secondary_activate()
129
24 by Umang Varma
Added .desktop and setup.py files.
130
    def new_note(self, *args):
131
        self.nset.new()
132
133
    def showall(self, *args):
134
        self.nset.showall(*args)
63 by Umang Varma
Middle-click toggles Show/Hide All.
135
        self.connect_secondary_activate()
24 by Umang Varma
Added .desktop and setup.py files.
136
137
    def hideall(self, *args):
138
        self.nset.hideall()
63 by Umang Varma
Middle-click toggles Show/Hide All.
139
        self.connect_secondary_activate()
140
141
    def connect_secondary_activate(self):
142
        """Define action of secondary action (middle click) depending
143
        on visibility state of notes."""
144
        if self.nset.properties["all_visible"] == True:
145
            self.ind.set_secondary_activate_target(self.mHideAll)
146
        else:
147
            self.ind.set_secondary_activate_target(self.mShowAll)
148
24 by Umang Varma
Added .desktop and setup.py files.
149
32 by Umang Varma
Save more often so changes aren't lost
150
    @save_required
24 by Umang Varma
Added .desktop and setup.py files.
151
    def lockall(self, *args):
152
        for note in self.nset.notes:
104 by Umang Varma
Fix some issues with gui = None
153
            note.set_locked_state(True)
24 by Umang Varma
Added .desktop and setup.py files.
154
        
32 by Umang Varma
Save more often so changes aren't lost
155
    @save_required
24 by Umang Varma
Added .desktop and setup.py files.
156
    def unlockall(self, *args):
157
        for note in self.nset.notes:
104 by Umang Varma
Fix some issues with gui = None
158
            note.set_locked_state(False)
24 by Umang Varma
Added .desktop and setup.py files.
159
41 by Umang Varma
Added about dialog
160
    def show_about(self, *args):
161
        show_about_dialog()
162
47 by Umang Varma
Added settings dialog, which controls bgcolor
163
    def show_settings(self, *args):
164
        wSettings = SettingsDialog(self.nset)
165
24 by Umang Varma
Added .desktop and setup.py files.
166
    def save(self):
167
        self.nset.save()
168
169
170
def main():
36 by Umang Varma
Start using python's locale for l10n
171
    try:
172
        locale.setlocale(locale.LC_ALL, '')
173
    except:
174
        locale.setlocale(locale.LC_ALL, 'C')
175
    # If we're running from /usr, then .mo files are not in MO_DIR.
176
    if os.path.abspath(__file__)[:4] == '/usr':
177
        # Fallback to default
178
        locale_dir = None
179
    else:
39 by Umang Varma
Find locale_dir correctly
180
        locale_dir = os.path.join(os.path.dirname(__file__), MO_DIR)
36 by Umang Varma
Start using python's locale for l10n
181
    locale.bindtextdomain(LOCALE_DOMAIN, locale_dir)
182
    locale.textdomain(LOCALE_DOMAIN)
62 by Umang Varma
Added -d parameter for development data file
183
77 by Umang Varma
Updated pot file and some strings
184
    parser = argparse.ArgumentParser(description=_("Sticky Notes"))
62 by Umang Varma
Added -d parameter for development data file
185
    parser.add_argument("-d", action='store_true', help="use the development"
186
            " data file")
187
    args = parser.parse_args()
188
189
    indicator = IndicatorStickyNotes(args)
46 by Umang Varma
Notes can now change color.
190
    # Load global css for the first time.
191
    load_global_css()
24 by Umang Varma
Added .desktop and setup.py files.
192
    Gtk.main()
193
    indicator.save()
194
195
if __name__ == "__main__":
196
    main()