~umang/indicator-stickynotes/trunk

« back to all changes in this revision

Viewing changes to indicator-stickynotes.py

  • Committer: Umang Varma
  • Date: 2012-06-24 20:43:03 UTC
  • Revision ID: git-v1:7154d9e47e06f4ca05d7f5a2acdd48d6d6efb3a7
Added .desktop and setup.py files.

In addition stick.py was moved to indicator-stickynotes.py so that it looks more
like its final installed name.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/python3
2
2
3
 
# Copyright © 2012-2013 Umang Varma <umang.me@gmail.com>
 
3
# Copyright © 2012 Umang Varma <umang.me@gmail.com>
4
4
5
5
# This file is part of indicator-stickynotes.
6
6
18
18
# indicator-stickynotes.  If not, see <http://www.gnu.org/licenses/>.
19
19
 
20
20
from stickynotes.backend import Note, NoteSet
21
 
from stickynotes.gui import *
22
 
import stickynotes.info
23
 
from stickynotes.info import MO_DIR, LOCALE_DOMAIN
24
 
 
 
21
from stickynotes.gui import StickyNote
25
22
from gi.repository import Gtk, Gdk
26
23
from gi.repository import AppIndicator3 as appindicator
27
24
 
28
 
import os.path
29
 
import locale
30
 
import argparse
31
 
from locale import gettext as _
32
 
from functools import wraps
33
 
from shutil import copyfile, SameFileError
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
43
 
 
44
25
class IndicatorStickyNotes:
45
 
    def __init__(self, args = None):
46
 
        self.args = args
47
 
        # use development data file if requested
48
 
        isdev = args and args.d
49
 
        self.data_file = stickynotes.info.DEBUG_SETTINGS_FILE if isdev \
50
 
                else stickynotes.info.SETTINGS_FILE
 
26
    def __init__(self):
51
27
        # Initialize NoteSet
52
 
        self.nset = NoteSet(StickyNote, self.data_file, self)
53
 
        try:
54
 
            self.nset.open()
55
 
        except FileNotFoundError:
56
 
            self.nset.load_fresh()
57
 
        except Exception as e:
58
 
            err = _("Error reading data file. Do you want to "
59
 
                "backup the current data?")
60
 
            winError = Gtk.MessageDialog(None, None, Gtk.MessageType.ERROR,
61
 
                    Gtk.ButtonsType.NONE, err)
62
 
            winError.add_buttons(Gtk.STOCK_CANCEL, Gtk.ResponseType.REJECT,
63
 
                    _("Backup"), Gtk.ResponseType.ACCEPT)
64
 
            resp = winError.run()
65
 
            winError.hide()
66
 
            if resp == Gtk.ResponseType.ACCEPT:
67
 
                self.backup_datafile()
68
 
            winError.destroy()
69
 
            self.nset.load_fresh()
70
 
 
71
 
        # If all notes were visible previously, show them now
72
 
        if self.nset.properties.get("all_visible", True):
73
 
            self.nset.showall()
 
28
        self.nset = NoteSet(StickyNote)
 
29
        self.nset.open()
 
30
        self.nset.showall()
74
31
        # Create App Indicator
75
32
        self.ind = appindicator.Indicator.new(
76
33
                "Sticky Notes", "indicator-stickynotes",
77
34
                appindicator.IndicatorCategory.APPLICATION_STATUS)
78
 
        # Delete/modify the following file when distributing as a package
79
 
        self.ind.set_icon_theme_path(os.path.abspath(os.path.join(
80
 
            os.path.dirname(__file__), 'Icons')))
81
 
        self.ind.set_icon("indicator-stickynotes")
82
35
        self.ind.set_status(appindicator.IndicatorStatus.ACTIVE)
83
 
        self.ind.set_title(_("Sticky Notes"))
 
36
        self.ind.set_title("Sticky Notes")
 
37
        #self.ind.set_attention_icon("pynagram")
84
38
        # Create Menu
85
39
        self.menu = Gtk.Menu()
86
 
        self.mNewNote = Gtk.MenuItem(_("New Note"))
 
40
        self.mNewNote = Gtk.MenuItem("New Note")
87
41
        self.menu.append(self.mNewNote)
88
42
        self.mNewNote.connect("activate", self.new_note, None)
89
43
        self.mNewNote.show()
92
46
        self.menu.append(s)
93
47
        s.show()
94
48
 
95
 
        self.mShowAll = Gtk.MenuItem(_("Show All"))
 
49
        self.mShowAll = Gtk.MenuItem("Show All")
96
50
        self.menu.append(self.mShowAll)
97
51
        self.mShowAll.connect("activate", self.showall, None)
98
52
        self.mShowAll.show()
99
53
 
100
 
        self.mHideAll = Gtk.MenuItem(_("Hide All"))
 
54
        self.mHideAll = Gtk.MenuItem("Hide All")
101
55
        self.menu.append(self.mHideAll)
102
56
        self.mHideAll.connect("activate", self.hideall, None)
103
57
        self.mHideAll.show()
106
60
        self.menu.append(s)
107
61
        s.show()
108
62
 
109
 
        self.mLockAll = Gtk.MenuItem(_("Lock All"))
 
63
        self.mLockAll = Gtk.MenuItem("Lock All")
110
64
        self.menu.append(self.mLockAll)
111
65
        self.mLockAll.connect("activate", self.lockall, None)
112
66
        self.mLockAll.show()
113
67
 
114
 
        self.mUnlockAll = Gtk.MenuItem(_("Unlock All"))
 
68
        self.mUnlockAll = Gtk.MenuItem("Unlock All")
115
69
        self.menu.append(self.mUnlockAll)
116
70
        self.mUnlockAll.connect("activate", self.unlockall, None)
117
71
        self.mUnlockAll.show()
120
74
        self.menu.append(s)
121
75
        s.show()
122
76
 
123
 
        self.mAbout = Gtk.MenuItem(_("About"))
124
 
        self.menu.append(self.mAbout)
125
 
        self.mAbout.connect("activate", self.show_about, None)
126
 
        self.mAbout.show()
127
 
 
128
 
        self.mSettings = Gtk.MenuItem(_("Settings"))
129
 
        self.menu.append(self.mSettings)
130
 
        self.mSettings.connect("activate", self.show_settings, None)
131
 
        self.mSettings.show()
132
 
 
133
 
        s = Gtk.SeparatorMenuItem.new()
134
 
        self.menu.append(s)
135
 
        s.show()
136
 
 
137
 
        self.mQuit = Gtk.MenuItem(_("Quit"))
 
77
        self.mQuit = Gtk.MenuItem("Quit")
138
78
        self.menu.append(self.mQuit)
139
79
        self.mQuit.connect("activate", Gtk.main_quit, None)
140
80
        self.mQuit.show()
141
81
        # Connect Indicator to menu
142
82
        self.ind.set_menu(self.menu)
143
83
 
144
 
        # Define secondary action (middle click)
145
 
        self.connect_secondary_activate()
146
 
 
147
 
    def backup_datafile(self):
148
 
        backupfile = show_export_file_chooser()
149
 
        if backupfile:
150
 
            try:
151
 
                copyfile(os.path.expanduser(self.data_file), backupfile)
152
 
            except SameFileError:
153
 
                err = _("Please choose a different "
154
 
                    "destination for the backup file.")
155
 
                winError = Gtk.MessageDialog(None, None,
156
 
                        Gtk.MessageType.ERROR, Gtk.ButtonsType.CLOSE, err)
157
 
                winError.run()
158
 
                winError.destroy()
159
 
                self.backup_datafile()
160
 
 
161
84
    def new_note(self, *args):
162
85
        self.nset.new()
163
86
 
164
87
    def showall(self, *args):
165
88
        self.nset.showall(*args)
166
 
        self.connect_secondary_activate()
167
89
 
168
90
    def hideall(self, *args):
169
91
        self.nset.hideall()
170
 
        self.connect_secondary_activate()
171
 
 
172
 
    def connect_secondary_activate(self):
173
 
        """Define action of secondary action (middle click) depending
174
 
        on visibility state of notes."""
175
 
        if self.nset.properties["all_visible"] == True:
176
 
            self.ind.set_secondary_activate_target(self.mHideAll)
177
 
        else:
178
 
            self.ind.set_secondary_activate_target(self.mShowAll)
179
 
 
180
 
 
181
 
    @save_required
 
92
 
182
93
    def lockall(self, *args):
183
94
        for note in self.nset.notes:
184
 
            note.set_locked_state(True)
 
95
            note.gui.set_locked_state(True)
185
96
        
186
 
    @save_required
187
97
    def unlockall(self, *args):
188
98
        for note in self.nset.notes:
189
 
            note.set_locked_state(False)
190
 
 
191
 
    def show_about(self, *args):
192
 
        show_about_dialog()
193
 
 
194
 
    def show_settings(self, *args):
195
 
        wSettings = SettingsDialog(self.nset)
 
99
            note.gui.set_locked_state(False)
196
100
 
197
101
    def save(self):
198
102
        self.nset.save()
199
103
 
200
104
 
201
105
def main():
202
 
    try:
203
 
        locale.setlocale(locale.LC_ALL, '')
204
 
    except:
205
 
        locale.setlocale(locale.LC_ALL, 'C')
206
 
    # If we're running from /usr, then .mo files are not in MO_DIR.
207
 
    if os.path.abspath(__file__)[:4] == '/usr':
208
 
        # Fallback to default
209
 
        locale_dir = None
210
 
    else:
211
 
        locale_dir = os.path.join(os.path.dirname(__file__), MO_DIR)
212
 
    locale.bindtextdomain(LOCALE_DOMAIN, locale_dir)
213
 
    locale.textdomain(LOCALE_DOMAIN)
214
 
 
215
 
    parser = argparse.ArgumentParser(description=_("Sticky Notes"))
216
 
    parser.add_argument("-d", action='store_true', help="use the development"
217
 
            " data file")
218
 
    args = parser.parse_args()
219
 
 
220
 
    indicator = IndicatorStickyNotes(args)
221
 
    # Load global css for the first time.
222
 
    load_global_css()
 
106
    indicator = IndicatorStickyNotes()
223
107
    Gtk.main()
224
108
    indicator.save()
225
109