Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/datastoretree.py
blob: 5260ef37d2a942373de25c8e44c4a7a552b01482 (plain)
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
#!/usr/bin/env python
# Copyright (C) 2008, One Laptop Per Child
#
# This program is jobjectree software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import os, sys, time

from sugar.datastore import datastore
from sugar import profile                                 
from sugar import util

from gettext import gettext as _

#major packages
import gtk
import gtk.glade
#import pydebug
#from pydebug import pydebug_instance

# Initialize logging.
import logging
from sugar import logger
#Get the standard logging directory. 
std_log_dir = logger.get_logs_dir()
_logger = logging.getLogger('PyDebug')

class DataStoreTree():
    column_names = [_('Name'), _('Size'), _('Last Changed')]
    
    def __init__(self, parent, widget=None,wTree=None):
        self.parent = parent
        self._activity = parent
        self.treeview = widget
        self.wTree = wTree
        self.init_model()
        self.init_columns()
        self.connect_object()
        self.limit=10
        self.journal_page_size =10
        self.journal_page_num = 0
        self.journal_max = 0
        self.new_directory()
        button = self.wTree.get_widget('from_journal')
        tt = gtk.Tooltips()
        tt.set_tip(button,_('Load the selected Journal XO (or tar.gz) file to the debug workplace'))
        #button.set_tooltip_text(_('Load the selected Journal XO (or tar.gz) file to the debug workplace'))
        button = self.wTree.get_widget('to_journal')
        tt = gtk.Tooltips()
        tt.set_tip(button,_('Zip up all the files in your debug workplace and store them in the Journal'))
        #button.set_tooltip_text(_('Zip up all the files in your debug workplace and store them in the Journal'))

        
    def connect_object(self,  wTree=None):
        mdict = {
                'younger_clicked_cb':self.newer_in_journal_cb,
                'older_clicked_cb':self.older_in_journal_cb,
                'from_journal_clicked_cb':self.load_from_journal_cb,
                'to_journal_clicked_cb':self.save_to_journal_cb ,
             }
        self.wTree.signal_autoconnect(mdict)
        
    def init_model(self):
        #plan for the store: pixbuf,title,size,last-updated,tooltip,jobject-id
        self.journal_model = gtk.TreeStore(gtk.gdk.Pixbuf, str,str,str,str,str,str,str,str,str,str)
        if not self.treeview:
            self.treeview = gtk.TreeView()
        self.treeview.set_model(self.journal_model)
        self.treeview.show()
        #self.treeview.has_tooltip = True
        #self.treeview.set_tooltip_column(10)
        #self.treeview.connect('query-tooltip',self.display_tooltip)
        self.show_hidden = False

    def init_columns(self):
        col = gtk.TreeViewColumn()
        col.set_title(self.column_names[0])
        render_pixbuf = gtk.CellRendererPixbuf()
        col.pack_start(render_pixbuf, expand=False)
        col.add_attribute(render_pixbuf, 'pixbuf', 0)
        render_text = gtk.CellRendererText()
        col.pack_start(render_text, expand=True)
        col.add_attribute(render_text, 'text', 1)
        col.set_fixed_width(20)
        self.treeview.append_column(col)
        cell = gtk.CellRendererText()
        col = gtk.TreeViewColumn(self.column_names[1], cell)
        col.add_attribute(cell, 'text', 2)
        self.treeview.append_column(col)
        cell = gtk.CellRendererText()
        col = gtk.TreeViewColumn(self.column_names[2], cell)
        col.add_attribute(cell, 'text', 3)
        self.treeview.append_column(col)
    
        
    def get_datastore_list(self, next=False):
        dslist = []
        """
        if not next:
            self.journal_page_num -= 1
        else:
            self.journal_page_num += 1
        if self.journal_page_num < 0: self.journal_page_num = 0
        if self.journal_max != 0:
            if self.journal_page_num * self.journal_page_size >= self.journal_max:
                self.journal_page_num -= 1
        #_logger.debug('fetch datastore data. limit:%s. offset: %s'%(self.limit,self.journal_page_num * self.journal_page_size))
        #(results,count)=datastore.find({'limit':self.limit,'offset':self.journal_page_num * self.journal_page_size})
        """
        self.journal_model.clear()
        ds_list = []
        num_found = 0
        mime_list = [self._activity.MIME_TYPE,'application/zip']
        
        #build 650 doesn't seem to understand correctly the dictionary with a list right hand side
        info = self._activity.util.sugar_version()
        if len(info)>0:
            (major,minor,micro,release) = info
            _logger.debug('sugar version major:%s minor:%s micro:%s release:%s'%info)
        else:
            _logger.debug('sugar version failure')
            minor = 70
        try:
            if minor > 80:
                (ds_list,num_found) = datastore.find({'mime_type': mime_list})
            else:
                (results,count) = datastore.find({'mime_type': self._activity.MIME_TYPE})
                ds_list.extend(results)
                num_found += count            
                (results,count) = datastore.find({'mime_type': 'application/zip'})
                ds_list.extend(results)
        except Exception,e:
            _logger.error('datastore error %s'%e)
            return []
        if num_found < self.limit:
            self.journal_max = self.journal_page_num * self.journal_page_size + num_found
        _logger.debug( 'datastoretree-get_datastore_list: count= %s'%num_found)
        keys = ('title','size','timestamp','activity','package','mime_type','file_path')
        for jobject in ds_list:
            itemlist = [None,]
            datastoredict=jobject.get_metadata().get_dictionary() #get the property dictionary
            src = jobject.get_file_path() #returns the full path of the file
            datastoredict['object_id'] = jobject.object_id
            
            if False: #if  src null, there is no file related to this meta data
                jobject.destroy()
                continue	#go get the next iterations
            #add in the info that we intend to use
            if src:
                info = os.stat(src) #get the directory information about this file
                datastoredict['size'] = info.st_size
            else:
                 datastoredict['size'] = 0               
            datastoredict['file_path'] = src
            for key in keys:
                if datastoredict.has_key(key):
                    if key == 'timestamp':
                        pkg = time.strftime('%Y-%m-%d %H:%M:%S',time.gmtime(float(datastoredict[key])))
                    elif key == 'title':
                        pkg = datastoredict[key][:18]
                    else:
                        pkg = '%s'%(datastoredict[key])
                else:
                    pkg = ''
                itemlist.append(pkg)
            itemlist.append(datastoredict['title'])
            itemlist.append(jobject.object_id)
            text = 'Mime_type: %s, Journal ID: %s, Bundle: %s'%(datastoredict.get('mime_type',''),
                                                                  datastoredict.get('object_id',''),
                                                                  datastoredict.get('package',''))
            itemlist.append(text)
            #_logger.debug('journal tooltip:%s'%text)
            dslist.append(itemlist)
            jobject.destroy()
        return dslist

    def new_directory(self,piter = None, next=True):
        journal_list = self.get_datastore_list(next)
        for journal_selected_data in journal_list:
            appended_iter = self.journal_model.append(piter, journal_selected_data )
            model=self.journal_model
            appended_path = model.get_path(appended_iter)
       
    def file_pixbuf(self, filename):
        folderpb = self.get_icon_pixbuf('STOCK_DIRECTORY')
        filepb = self.get_icon_pixbuf('STOCK_FILE')
        if os.path.isdir(filename):
            pb = folderpb
        else:
            pb = filepb
        return pb

    def get_icon_pixbuf(self, stock):
        return self.treeview.render_icon(stock_id=getattr(gtk, stock),
                                size=gtk.ICON_SIZE_MENU,
                                detail=None)

    def get_treeview(self):
        return self.treeview

    def newer_in_journal_cb(self,button):
        _logger.debug('younger call back')
        self.get_datastore_list(next=False)
        
    def older_in_journal_cb(self,button):
        _logger.debug('older call back')
        self.get_datastore_list(next=True)
        
    def save_to_journal_cb(self,button):
        self.parent.proj_funct.write_binary_to_datastore()
        
    def load_from_journal_cb(self,button):
        selection=self.treeview.get_selection()
        (model,iter)=selection.get_selected()
        if iter == None:
            self.parent.alert(_('Must select Journal item to Load'))
            return
        object_id = model.get(iter,9)
        _logger.debug('object id %s from journal'%object_id)
        self.parent.proj_funct.try_to_load_from_journal(object_id)
        
    def datastore_row_activated_cb(self):
        self.load_from_journal_cb()
    
    def display_tooltip(self, widget,x,y,mode,tooltip):
        _logger.debug('in display_tooltip')
        tooltip.show()
        return True
        
def main():
    gtk.main()

if __name__ == "__main__":
        # Create a new window
    """window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    window.set_size_request(400, 300)
    window.show()
    """
    wTree=gtk.glade.XML('/home/olpc/Activities/PyDebug.activity/project.glade')
    top = wTree.get_widget('toplevel')
    top.show()
    file_system = wTree.get_widget('journal')

    #pydebug.Activity.set_canvas(pydebug_instance,self.contents)
    flcdexample = DataStoreTree(None, file_system,wTree)
             
    #flcdexample.connect_object(mdict)
    main()