~macaco-dev/macaco/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/usr/bin/python
# -*- coding: utf-8 -*-
### BEGIN LICENSE
# Copyright (C) 2009 Manuel de la Pena <mandel@themacaque.com>
#This program is free software: you can redistribute it and/or modify it 
#under the terms of the GNU General Public License version 3, as published 
#by the Free Software Foundation.
#
#This program is distributed in the hope that it will be useful, but 
#WITHOUT ANY WARRANTY; without even the implied warranties of 
#MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
#PURPOSE.  See the GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License along 
#with this program.  If not, see <http://www.gnu.org/licenses/>.
### END LICENSE

import sys
import os
import gtk

# Check if we are working in the source tree or from the installed 
# package and mangle the python path accordingly
if os.path.dirname(sys.argv[0]) != ".":
    if sys.argv[0][0] == "/":
        fullPath = os.path.dirname(sys.argv[0])
    else:
        fullPath = os.getcwd() + "/" + os.path.dirname(sys.argv[0])
else:
    fullPath = os.getcwd()
sys.path.insert(0, os.path.dirname(fullPath))

from harmony import AboutHarmonyDialog, PreferencesHarmonyDialog
from harmony.ContactDataDialog import NewContactDataDialog
from harmony.harmonyconfig import getdatapath
from harmony.contacts import contact_name, minimize_contact
from desktopcouch.contacts import ContactRepository, Contact
    
class HarmonyWindow(gtk.Window):
    __gtype_name__ = "HarmonyWindow"

    def __init__(self):
        """__init__ - This function is typically not called directly.
        Creation a HarmonyWindow requires redeading the associated ui
        file and parsing the ui definition extrenally,
        and then calling HarmonyWindow.finish_initializing().

        Use the convenience function NewHarmonyWindow to create
        HarmonyWindow object.

        """
        pass

    def finish_initializing(self, builder):
        """finish_initalizing should be called after parsing the ui definition
        and creating a HarmonyWindow object with it in order to finish
        initializing the start of the new HarmonyWindow instance.

        """
        #get a reference to the builder and set up the signals
        self.builder = builder
        self.builder.connect_signals(self)
        
        #uncomment the following code to read in preferences at start up
        #dlg = PreferencesHarmonyDialog.NewPreferencesHarmonyDialog()
        #self.preferences = dlg.get_preferences()
        
        self.contacts_store = self.builder.get_object("contacts_store")
        self.tabbed_panel = self.builder.get_object("tabbed_panel")
        self.groups = self.builder.get_object("groups_layout")
        self.groups_menu_item = self.builder.get_object("groups_menu_item")
        self.actions_menu = self.builder.get_object("actions_menu")
        # set a variables that will used to know which was the last used
        # tab, that is useful to allow to work with the last tab
        self.last_tab = 0
        # we set the activated object to allow to use the double 
        # click to be used to see contacts information
        self.selected = None
        # use a var to remeber the position of the panel handler
        #self.panel_position = self.groups_panel.get_position()
        # set the views info
        self.alphabetical_view = self.builder.get_object("alphabetical_view")
        self.name_surname_view = self.builder.get_object("name_surname_view")
        self.frecuently_used_view = self.builder.get_object("frecuently_used_view")
        
        for view in (self.alphabetical_view, 
                     self.name_surname_view, self.frecuently_used_view): 
            view.set_text_column(0)
            view.set_pixbuf_column(1)
        
        self.repo = ContactRepository("contacts", create=True)
        # we set the sort funtion to be alphabetical name surname A-Z
        self.contacts_store.set_sort_column_id(0, gtk.SORT_ASCENDING)
        # we are going to loop and add the data to the store, we just create
        # contacts with the min amount of data
        for current_contact in self.repo.contacts:
            minimize_contact(current_contact)
            self.add_contact(current_contact)

    def tabbed_panel_switch_page_cb(self, tab_panel, event, page_index):
        # work with the last tab to deselect anything selected
        if self.last_tab == 0:
            self.alphabetical_view.unselect_all()
        elif self.last_tab == 1:
            self.name_surname_view.unselect_all()
        else:
            self.frecuently_used_view.unselect_all()
            
        # change the type of sorting used
        if page_index == 0:
            self.contacts_store.set_sort_column_id(0, gtk.SORT_ASCENDING)
        elif page_index == 1:
            self.contacts_store.set_sort_column_id(3, gtk.SORT_ASCENDING)
        else:
            self.contacts_store.set_sort_column_id(4, gtk.SORT_DESCENDING)
        self.last_tab = page_index
        
    def get_selected_items(self):
        """
        Returns the selected items according to the tab used.
        """
        current_page = self.tabbed_panel.get_current_page()
        paths = []
        if current_page == 0:
            paths = self.alphabetical_view.get_selected_items()
        elif current_page == 1:
            paths = self.name_surname_view.get_selected_items()
        else:
            paths = self.frecuently_used_view.get_selected_items()
        return paths
        
    def add_contact(self, contact):
        """
        Adds a new contact to be shown in the UI.
        """
        # TODO: Use the contact avatar
        image = os.path.join(getdatapath(), 'media', 'contact.png')
        icon = gtk.gdk.pixbuf_new_from_file_at_size(image, 60, 60)
        if hasattr(contact, "times_viewed"):
            times = contact.times_viewed
        else:
            times = 0    
        self.contacts_store.append([contact_name(contact), icon, contact.first_name, 
            contact.last_name, times, contact])
        
    def new_contact(self, event):
        """
        Allows the user to create a new contact by showing a dialog to the
        user.
        """
        dialog = NewContactDataDialog()
        # change the text of the button, is the same ui but just change update
        # for ok
        dialog.update_button.set_label("Ok")
        result = dialog.run()
        dialog.destroy()
        if result == gtk.RESPONSE_OK:
            self.add_contact(dialog.contact)
            
    def remove_contact(self, event):
        """
        Removes the selected contacts from the view and the db.
        """
        current_page = self.tabbed_panel.get_current_page()
        paths = self.get_selected_items()
        if len(paths) == 0:
            dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL, 
                gtk.MESSAGE_WARNING, gtk.BUTTONS_OK, 
                "Please select a contact or contacts to remove.")
            dialog.run()
            dialog.destroy()
        else:    
            dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL, 
                     gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO, 
                     "Do you want to remove the selected contacts?")
            result = dialog.run()
            dialog.destroy()
            if result == gtk.RESPONSE_YES:
                # we are going to get the currect selected tab to know the 
                # selected contacts, then we remove them.
                contacts = []
                for current_path in paths:
                    tree_iter = self.contacts_store.get_iter(current_path)
                    # we get the contacts and use the repo to remove them.
                    current_contact = self.contacts_store.get_value(tree_iter, 5)
                    contacts.append(current_contact)
                    self.contacts_store.remove(tree_iter)
                self.repo.remove(contacts)
                dialog.destroy()
    
    def first_button_clicked(self, item, event):
        """
        Action performed when the user uses the first button to click on an icon
        """
        # we check the number of selected items, if it is more than 0 we do not
        # perform the action
        paths = self.get_selected_items()
        if not len(paths) != 1:
            if self.selected is None:
                self.selected = paths[0]
            else:
                # we check if it is the same, if it is we show the info, 
                # otherwhise, change the value
                if self.selected == paths[0]:
                    # we get the contact and show it in the dialog.
                    tree_iter = self.contacts_store.get_iter(paths[0])
                    # we get the contacts and use the repo to remove them.
                    contact_id = self.contacts_store.get_value(tree_iter, 5)._id
                    contact = self.repo.get_by_id(contact_id)
                    dialog = NewContactDataDialog(contact)
                    result = dialog.run()
                    if result == gtk.RESPONSE_OK:
                        # TODO: Use contact avatar!
                        # we minimize the contact, the data is not needed in
                        # this ui
                        minimize_contact(dialog.contact)
                        self.contacts_store.set(tree_iter, 
                            0, contact_name(dialog.contact), 
                            # 1, avatar
                            2, dialog.contact.first_name, 
                            3, dialog.contact.last_name, 
                            4, dialog.contact.times_viewed)
                    else:
                        # we store the number of time the contact was viewed
                        self.repo.add(dialog.contact)
                        self.contacts_store.set(tree_iter, 
                            4, dialog.contact.times_viewed)
                else:
                    self.selected = paths[0]
                    
    def second_button_clicked(self, item, event):
        """
        Shows the actions menu which allows the user to work with their contacts
        in a way or an other.
        """
        self.actions_menu.popup( None, None, None, event.button, event.time)
        
    def icon_view_button_release(self, item, event):
        """
        Action performed when the user used the mouse in one of the views.
        """
        if event.button == 1:
            self.first_button_clicked(item, event)
        elif event.button == 3:
            self.second_button_clicked(item, event)
    
    def groups_menu_item_toggled_cb(self, event):
        active = self.groups_menu_item.get_active()
        if active:
            self.show_panel()
        else:
            self.close_panel()
            
    def close_panel(self, *args):
        """
        Simple closes the panel.
        """
        self.groups.props.visible = False
    
    def show_panel(self, *args):
        self.groups.props.visible = True 
       
    def about(self, widget, data=None):
        """about - display the about box for harmony """
        about = AboutHarmonyDialog.NewAboutHarmonyDialog()
        response = about.run()
        about.destroy()

    def preferences(self, widget, data=None):
        """preferences - display the preferences window for harmony """
        prefs = PreferencesHarmonyDialog.NewPreferencesHarmonyDialog()
        response = prefs.run()
        if response == gtk.RESPONSE_OK:
            #make any updates based on changed preferences here
            pass
        prefs.destroy()

    def quit(self, widget, data=None):
        """quit - signal handler for closing the HarmonyWindow"""
        self.destroy()

    def on_destroy(self, widget, data=None):
        """on_destroy - called when the HarmonyWindow is close. """
        gtk.main_quit()

def NewHarmonyWindow():
    """NewHarmonyWindow - returns a fully instantiated
    HarmonyWindow object. Use this function rather than
    creating a HarmonyWindow directly.
    """

    #look for the ui file that describes the ui
    ui_filename = os.path.join(getdatapath(), 'ui', 'HarmonyWindow.ui')
    if not os.path.exists(ui_filename):
        ui_filename = None

    builder = gtk.Builder()
    builder.add_from_file(ui_filename)
    window = builder.get_object("harmony_window")
    window.finish_initializing(builder)
    return window

if __name__ == "__main__":
    # set the application name to allow application annotations
    Contact.application_name = "harmony"
    #support for command line options
    import logging, optparse
    parser = optparse.OptionParser(version="%prog %ver")
    parser.add_option("-v", "--verbose", action="store_true", dest="verbose", help="Show debug messages")
    (options, args) = parser.parse_args()

    #set the logging level to show debug messages
    if options.verbose:
        logging.basicConfig(level=logging.DEBUG)
        logging.debug('logging enabled')

    #run the application
    window = NewHarmonyWindow()
    window.show()
    gtk.main()