Add files from the old svn, r101.
[matthijs/upstream/mobilegtd.git] / src / model / project.py
1 from action import Action
2 from info import Info
3 from model import *
4 from datetime import date
5 import action
6 from observable import *
7 from filtered_list import FilteredList,StatusFilteredList
8 import datetime
9 from log.logging import logger
10 import sys
11
12 class ProjectStatus(Status):
13     pass
14
15 #    def __eq__(self,other):
16 #        return False
17 class Inactive(ProjectStatus):
18     def __init__(self):
19         super(Inactive,self).__init__(u'review',4,'!')
20
21     def update(self,project):
22 #        if project.has_active_actions():
23 #            #print repr(active)
24 #            return active
25         return self
26
27
28
29 class Active(ProjectStatus):
30     
31     def __init__(self):
32         super(Active,self).__init__(u'active',1)
33         
34     def update(self,project):
35         if not project.has_active_actions():
36             #print repr(inactive)
37             return inactive
38         return self
39
40     
41 class Tickled(ProjectStatus):
42     def __init__(self,date=date.tomorrow()):
43         super(Tickled,self).__init__(u'tickled',3,'/')
44         self.date = date
45     
46     def update(self,project):
47         if self.date <= date.now():
48             return active
49         else:
50             return self
51
52     def __str__(self):
53         return super(Tickled,self).__str__()+" for %s"%self.date
54     
55     def __repr__(self):
56         return self.__str__()
57
58 unprocessed = ProjectStatus(u'unprocessed',0)
59 active = Active()
60 done = ProjectStatus(u'done',2,'+')
61 tickled = Tickled()
62 inactive = Inactive()
63 someday = ProjectStatus(u'someday',5,'~')
64 info = ProjectStatus(u'info',0)
65
66
67
68 class Project(ObservableItem,ItemWithStatus):
69     observers = []
70     def __init__(self,name,status = inactive):
71         assert type(name) == unicode
72         logger.log(u'Creating project %s (%s)'%(name,status))
73         ItemWithStatus.__init__(self,status)
74         self.name=name
75         self.actions=StatusFilteredList([])
76         self.infos=FilteredList([])
77         self.update_methods = {'status':self.action_changed_status,
78                                'description':self.action_changed_content,
79                                'info':self.action_changed_content,
80                                'context':self.action_changed_content,
81                                'text':self.info_changed}
82         super(Project,self).__init__()
83         for o in Project.observers:
84             o.notify(self.__class__,'new_project',self,None)
85         logger.log(u'Now, its project %s (%s)'%(name,status))
86
87
88     def add_action(self,a):
89         a.project = self
90         a.observers.append(self)
91         self.actions.append(a)
92         self.notify_observers('add_action',a)
93         if a.status == action.unprocessed:
94             a.status = action.active
95         
96     def remove_action(self,a):
97         a.status = action.done
98         a.observers.remove(self)
99         self.actions.remove(a)
100         self.notify_observers('remove_action',a)
101
102     def add_info(self,info,position=None):
103         info.observers.append(self)
104         self.infos.append(info)
105         self.notify_observers('add_info', info)
106
107     def remove_info(self,info):
108         info.observers.remove(self)
109         self.infos.remove(info)
110         self.notify_observers('remove_info', info)
111
112     def activate(self):
113         self.status = active
114         for a in self.actions_with_status(action.inactive):
115             a.status = action.active
116
117     def deactivate(self):
118         self.status = inactive
119         for a in self.actions_with_status(action.active):
120             a.status = action.inactive
121
122     def actions_with_status(self,status):
123         return self.actions.with_property(lambda a:a.status == status)
124
125     def active_actions(self):
126         return self.actions_with_status(action.active)
127
128     def has_active_actions(self):
129         return len(self.active_actions()) > 0
130     
131     def notify(self,action,attribute,new=None,old=None):
132         self.update_methods[attribute](action,new)
133     
134     def info_changed(self,info,text):
135         self.notify_observers('changed_info', info)
136
137     def action_changed_content(self,action,content):
138         self.notify_observers('changed_action',action)   
139     
140     def action_changed_status(self,a,status):
141         self.notify_observers('changed_action', new=a, old=None)
142         
143     def last_modification_date(self):
144         return datetime.date.now()
145         
146     def __eq__(self, other):
147         return self.name == other.name and self.status == other.status
148
149     def __ne__(self,project):
150         return not self.__eq__(project)
151
152     def __str__(self):
153         return self.name
154
155     def status_symbol_and_name(self):
156         return self.status_symbol()+self.name
157     def __repr__(self):
158         return u'Project %s (@%s, %s actions, %s infos)'%("Moeject",self.status.name.capitalize(),len(self.actions),len(self.infos))