~umang/indicator-stickynotes/trunk

« back to all changes in this revision

Viewing changes to stickynotes/backend.py

  • Committer: Umang Varma
  • Date: 2012-06-23 15:22:32 UTC
  • Revision ID: git-v1:8a2acc262a26ec4423079701a1be9e3c429f6ba0
Move backend and gui to sticknotes package

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright © 2012 Umang Varma <umang.me@gmail.com>
 
2
 
3
# This file is part of indicator-stickynotes.
 
4
 
5
# indicator-stickynotes is free software: you can redistribute it and/or
 
6
# modify it under the terms of the GNU General Public License as published by
 
7
# the Free Software Foundation, either version 3 of the License, or (at your
 
8
# option) any later version.
 
9
 
10
# indicator-stickynotes is distributed in the hope that it will be useful, but
 
11
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
12
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 
13
# more details.
 
14
 
15
# You should have received a copy of the GNU General Public License along with
 
16
# indicator-stickynotes.  If not, see <http://www.gnu.org/licenses/>.
 
17
 
 
18
from datetime import datetime
 
19
import uuid
 
20
import json
 
21
from os.path import expanduser
 
22
 
 
23
SETTINGS_FILE = "stickynotesrc"
 
24
 
 
25
class Note:
 
26
    def __init__(self, content=None, gui_class=None, noteset=None):
 
27
        content = content or {}
 
28
        self.uuid = content.get('uuid')
 
29
        self.body = content.get('body','')
 
30
        self.properties = content.get("properties", {})
 
31
        last_modified = content.get('last_modified')
 
32
        if last_modified:
 
33
            self.last_modified = datetime.strptime(last_modified,
 
34
                    "%Y-%m-%dT%H:%M:%S")
 
35
        else:
 
36
            self.last_modified = datetime.now()
 
37
        self.gui_class = gui_class
 
38
        self.gui = None
 
39
        self.noteset = noteset
 
40
 
 
41
    def extract(self):
 
42
        self.gui.update_note()
 
43
        if not self.uuid:
 
44
            self.uuid = str(uuid.uuid4())
 
45
        self.properties = self.gui.properties()
 
46
        return {"uuid":self.uuid, "body":self.body,
 
47
                "last_modified":self.last_modified.strftime(
 
48
                    "%Y-%m-%dT%H:%M:%S"), "properties":self.properties}
 
49
 
 
50
    def update(self,body=None):
 
51
        if not body == None:
 
52
            self.body = body
 
53
            self.last_modified = datetime.now()
 
54
 
 
55
    def delete(self):
 
56
        self.noteset.notes.remove(self)
 
57
        self.noteset.save()
 
58
        del self
 
59
 
 
60
    def show(self, *args):
 
61
        if not self.gui:
 
62
            self.gui = self.gui_class(note=self)
 
63
        self.gui.show(*args)
 
64
 
 
65
    def hide(self):
 
66
        self.gui.hide()
 
67
 
 
68
 
 
69
class NoteSet:
 
70
    def __init__(self, gui_class):
 
71
        self.notes = []
 
72
        self.gui_class = gui_class
 
73
 
 
74
    def _loads_updater(self, dnoteset):
 
75
        """Parses old versions of the Notes structure and updates them"""
 
76
        return dnoteset
 
77
 
 
78
    def loads(self, snoteset):
 
79
        """Loads notes into their respective objects"""
 
80
        notes = self._loads_updater(json.loads(snoteset))
 
81
        self.notes = [Note(note, gui_class=self.gui_class, noteset=self)
 
82
                for note in notes.get("notes",[])]
 
83
 
 
84
    def dumps(self):
 
85
        return json.dumps({"notes":[x.extract() for x in self.notes]})
 
86
 
 
87
    def save(self, path=''):
 
88
        output = self.dumps()
 
89
        with open(path or expanduser("~/.{0}".format(SETTINGS_FILE)),
 
90
                mode='w', encoding='utf-8') as fsock:
 
91
            fsock.write(output)
 
92
 
 
93
    def open(self, path=''):
 
94
        with open(path or expanduser("~/.{0}".format(SETTINGS_FILE)), 
 
95
                encoding='utf-8') as fsock:
 
96
            self.loads(fsock.read())
 
97
 
 
98
    def new(self):
 
99
        """Creates a new note and adds it to the note set"""
 
100
        note = Note(gui_class=self.gui_class, noteset=self)
 
101
        self.notes.append(note)
 
102
        note.show()
 
103
        return note
 
104
 
 
105
    def showall(self, *args):
 
106
        for note in self.notes:
 
107
            note.show(*args)
 
108
 
 
109
    def hideall(self, *args):
 
110
        self.save()
 
111
        for note in self.notes:
 
112
            note.hide(*args)
 
113
 
 
114
class dGUI:
 
115
    def __init__(self, *args, **kwargs):
 
116
        pass
 
117
    """Dummy GUI"""
 
118
    def show(self):
 
119
        pass
 
120
    def hide(self):
 
121
        pass
 
122
    def update_note(self):
 
123
        pass
 
124
    def properties(self):
 
125
        return None
 
126