First commit. Here we go.
[canola-rtm] / canola-rtm / rtm / model.py
1 # Canola2 Remember The Milk Plugin
2 # Authors: Andrey Popelo <andrey@popelo.com>
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 #
17 # Additional permission under GNU GPL version 3 section 7
18 #
19 # If you modify this Program, or any covered work, by linking or combining it
20 # with Canola2 and its core components (or a modified version of any of those),
21 # containing parts covered by the terms of Instituto Nokia de Tecnologia End
22 # User Software Agreement, the licensors of this Program grant you additional
23 # permission to convey the resulting work.
24
25 import logging
26
27 from terra.core.manager import Manager
28 from terra.core.task import Task
29 from terra.core.model import ModelFolder, Model
30
31 from client import Client
32
33
34 manager = Manager()
35 PluginDefaultIcon = manager.get_class("Icon/Plugin")
36
37 log = logging.getLogger("plugins.canola-rtm.model")
38
39
40 class Icon(PluginDefaultIcon):
41     terra_type = "Icon/Folder/Task/Apps/RTM"
42     icon = "icon/main_item/rtm"
43     plugin = "rtm"
44
45 # TODO more documentation
46 class RTMTaskModel(Model):
47     """Base class for Remember The Milk task models.
48
49     """
50     terra_type = "Model/RTMTask"
51
52     def __init__(self, name, parent=None):
53         Model.__init__(self, name, parent)
54         self.duedate = ""
55         self.priority = ""
56
57 class ServiceModelFolder(ModelFolder):
58     terra_type = "Model/Folder/Task/Apps/RTM/Service"
59
60     empty_msg = "No tasks found"
61
62     def __init__(self, name, parent):
63         ModelFolder.__init__(self, name, parent)
64         # TODO change token
65         self.client = Client('1e6e489de9374b43ba280ab9741d290c', 'c7197d30f722247d', '1f4c8846175f4bafcf70f7742f4670e41b0ab046')
66
67     def do_load(self):
68         self.search()
69
70     def search(self):
71         del self.children[:]
72
73         for c in self.do_search():
74             self.children.append(c)
75         return
76
77     def do_search(self):
78         raise NotImplementedError("must be implemented by subclasses")
79
80 ##############################################################################
81 # Remember The Milk Remote Service Models
82 ##############################################################################
83
84 class TodayModelFolder(ServiceModelFolder):
85     """This model implements the Today option."""
86     terra_type = "Model/Folder/Task/Apps/RTM/Service/Today"
87
88     def __init__(self, name, parent):
89         ServiceModelFolder.__init__(self, name, parent)
90
91     def do_search(self):
92         lst = []
93         self.client.tasks.getList(filter='dueBefore:tomorrow')
94         # TODO remove stubs
95         lst = [ RTMTaskModel("Task #1 (stub)"),
96                 RTMTaskModel("Task #2 (stub)"),
97                 RTMTaskModel("Task #3 (stub)") ]
98
99         return lst
100
101
102 ##############################################################################
103 # Main Remember The Milk Model
104 ##############################################################################
105
106 class MainModelFolder(ModelFolder, Task):
107     """Main Remember The Milk Model.
108
109     This is the main remember the milk model. It initializes all other models.
110     """
111     terra_type = "Model/Folder/Task/Apps/RTM"
112     terra_task_type = "Task/Folder/Task/Apps/RTM"
113
114     def __init__(self, parent):
115         Task.__init__(self)
116         ModelFolder.__init__(self, "Remember the milk", parent)
117
118     def do_load(self):
119         """Initialize base remember the milk models, like Today, Smart
120         lists, Search and others.
121         """
122         TodayModelFolder("Today", self);
123