~umang/indicator-stickynotes/trunk

24 by Umang Varma
Added .desktop and setup.py files.
1
#!/usr/bin/python3
2
# 
3
# Copyright © 2012 Umang Varma <umang.me@gmail.com>
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
36 by Umang Varma
Start using python's locale for l10n
23
from stickynotes.info import MO_DIR, LOCALE_DOMAIN
32 by Umang Varma
Save more often so changes aren't lost
24
24 by Umang Varma
Added .desktop and setup.py files.
25
from gi.repository import Gtk, Gdk
26
from gi.repository import AppIndicator3 as appindicator
32 by Umang Varma
Save more often so changes aren't lost
27
30 by Umang Varma
Added ubuntu-mono themed icons.
28
import os.path
36 by Umang Varma
Start using python's locale for l10n
29
import locale
30
from locale import gettext as _
32 by Umang Varma
Save more often so changes aren't lost
31
from functools import wraps
32
33
def save_required(f):
34
    """Wrapper for functions that require a save after execution"""
35
    @wraps(f)
36
    def _wrapper(self, *args, **kwargs):
37
        ret = f(self, *args, **kwargs)
38
        self.save()
39
        return ret
40
    return _wrapper
24 by Umang Varma
Added .desktop and setup.py files.
41
42
class IndicatorStickyNotes:
43
    def __init__(self):
44
        # Initialize NoteSet
45
        self.nset = NoteSet(StickyNote)
46
        self.nset.open()
47
        self.nset.showall()
48
        # Create App Indicator
49
        self.ind = appindicator.Indicator.new(
50
                "Sticky Notes", "indicator-stickynotes",
51
                appindicator.IndicatorCategory.APPLICATION_STATUS)
30 by Umang Varma
Added ubuntu-mono themed icons.
52
        # Delete/modify the following file when distributing as a package
53
        self.ind.set_icon_theme_path(os.path.abspath(os.path.join(
54
            os.path.dirname(__file__), 'Icons')))
55
        self.ind.set_icon("indicator-stickynotes")
24 by Umang Varma
Added .desktop and setup.py files.
56
        self.ind.set_status(appindicator.IndicatorStatus.ACTIVE)
36 by Umang Varma
Start using python's locale for l10n
57
        self.ind.set_title(_("Sticky Notes"))
24 by Umang Varma
Added .desktop and setup.py files.
58
        # Create Menu
59
        self.menu = Gtk.Menu()
36 by Umang Varma
Start using python's locale for l10n
60
        self.mNewNote = Gtk.MenuItem(_("New Note"))
24 by Umang Varma
Added .desktop and setup.py files.
61
        self.menu.append(self.mNewNote)
62
        self.mNewNote.connect("activate", self.new_note, None)
63
        self.mNewNote.show()
64
65
        s = Gtk.SeparatorMenuItem.new()
66
        self.menu.append(s)
67
        s.show()
68
36 by Umang Varma
Start using python's locale for l10n
69
        self.mShowAll = Gtk.MenuItem(_("Show All"))
24 by Umang Varma
Added .desktop and setup.py files.
70
        self.menu.append(self.mShowAll)
71
        self.mShowAll.connect("activate", self.showall, None)
72
        self.mShowAll.show()
73
36 by Umang Varma
Start using python's locale for l10n
74
        self.mHideAll = Gtk.MenuItem(_("Hide All"))
24 by Umang Varma
Added .desktop and setup.py files.
75
        self.menu.append(self.mHideAll)
76
        self.mHideAll.connect("activate", self.hideall, None)
77
        self.mHideAll.show()
78
79
        s = Gtk.SeparatorMenuItem.new()
80
        self.menu.append(s)
81
        s.show()
82
36 by Umang Varma
Start using python's locale for l10n
83
        self.mLockAll = Gtk.MenuItem(_("Lock All"))
24 by Umang Varma
Added .desktop and setup.py files.
84
        self.menu.append(self.mLockAll)
85
        self.mLockAll.connect("activate", self.lockall, None)
86
        self.mLockAll.show()
87
36 by Umang Varma
Start using python's locale for l10n
88
        self.mUnlockAll = Gtk.MenuItem(_("Unlock All"))
24 by Umang Varma
Added .desktop and setup.py files.
89
        self.menu.append(self.mUnlockAll)
90
        self.mUnlockAll.connect("activate", self.unlockall, None)
91
        self.mUnlockAll.show()
92
93
        s = Gtk.SeparatorMenuItem.new()
94
        self.menu.append(s)
95
        s.show()
96
41 by Umang Varma
Added about dialog
97
        self.mAbout = Gtk.MenuItem(_("About"))
98
        self.menu.append(self.mAbout)
99
        self.mAbout.connect("activate", self.show_about, None)
100
        self.mAbout.show()
101
47 by Umang Varma
Added settings dialog, which controls bgcolor
102
        self.mSettings = Gtk.MenuItem(_("Settings"))
103
        self.menu.append(self.mSettings)
104
        self.mSettings.connect("activate", self.show_settings, None)
105
        self.mSettings.show()
106
41 by Umang Varma
Added about dialog
107
        s = Gtk.SeparatorMenuItem.new()
108
        self.menu.append(s)
109
        s.show()
110
36 by Umang Varma
Start using python's locale for l10n
111
        self.mQuit = Gtk.MenuItem(_("Quit"))
24 by Umang Varma
Added .desktop and setup.py files.
112
        self.menu.append(self.mQuit)
113
        self.mQuit.connect("activate", Gtk.main_quit, None)
114
        self.mQuit.show()
115
        # Connect Indicator to menu
116
        self.ind.set_menu(self.menu)
117
118
    def new_note(self, *args):
119
        self.nset.new()
120
121
    def showall(self, *args):
122
        self.nset.showall(*args)
123
124
    def hideall(self, *args):
125
        self.nset.hideall()
126
32 by Umang Varma
Save more often so changes aren't lost
127
    @save_required
24 by Umang Varma
Added .desktop and setup.py files.
128
    def lockall(self, *args):
129
        for note in self.nset.notes:
130
            note.gui.set_locked_state(True)
131
        
32 by Umang Varma
Save more often so changes aren't lost
132
    @save_required
24 by Umang Varma
Added .desktop and setup.py files.
133
    def unlockall(self, *args):
134
        for note in self.nset.notes:
135
            note.gui.set_locked_state(False)
136
41 by Umang Varma
Added about dialog
137
    def show_about(self, *args):
138
        show_about_dialog()
139
47 by Umang Varma
Added settings dialog, which controls bgcolor
140
    def show_settings(self, *args):
141
        wSettings = SettingsDialog(self.nset)
142
24 by Umang Varma
Added .desktop and setup.py files.
143
    def save(self):
144
        self.nset.save()
145
146
147
def main():
36 by Umang Varma
Start using python's locale for l10n
148
    try:
149
        locale.setlocale(locale.LC_ALL, '')
150
    except:
151
        locale.setlocale(locale.LC_ALL, 'C')
152
    # If we're running from /usr, then .mo files are not in MO_DIR.
153
    if os.path.abspath(__file__)[:4] == '/usr':
154
        # Fallback to default
155
        locale_dir = None
156
    else:
39 by Umang Varma
Find locale_dir correctly
157
        locale_dir = os.path.join(os.path.dirname(__file__), MO_DIR)
36 by Umang Varma
Start using python's locale for l10n
158
    locale.bindtextdomain(LOCALE_DOMAIN, locale_dir)
159
    locale.textdomain(LOCALE_DOMAIN)
24 by Umang Varma
Added .desktop and setup.py files.
160
    indicator = IndicatorStickyNotes()
46 by Umang Varma
Notes can now change color.
161
    # Load global css for the first time.
162
    load_global_css()
24 by Umang Varma
Added .desktop and setup.py files.
163
    Gtk.main()
164
    indicator.save()
165
166
if __name__ == "__main__":
167
    main()