Changeset 32 for storage

Show
Ignore:
Timestamp:
07/09/06 14:53:14 (2 years ago)
Author:
ploum
Message:

#11 Bookmarks are not stored

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • storage/bookmarks_store.py

    r31 r32  
    11#!/usr/bin/env python 
    22# -*- coding: utf-8 -*- 
    3 import sys, string, xml.dom.minidom 
     3import sys, string, xml.dom.minidom, os 
    44try: 
    55        import gtk 
     
    88        sys.exit(1) 
    99 
     10bmark_file="bookmarks.xml" 
     11 
    1012# this class represent the whole bookmarks list 
    1113class bookmarks_store : 
     
    1315        # differents bookmarks for differents protocols. 
    1416        def __init__(self, bts_name) : 
    15                 #listing is : Title of the bookmarks, number of the search method, arguments of the search 
     17                #listing is : Title of the bookmarks, description of the search 
    1618                # search method : 
    1719                # 1- bug number 
     
    1921                # 3- package search 
    2022                # 4- advanced search 
    21                 self.listing = gtk.ListStore(gobject.TYPE_STRING,gobject.TYPE_INT, gobject.TYPE_STRING) 
    22                 # essai temporaire 
    23                 # normalement ici on lit le fichier de préférences 
    24                 self.listing.insert_before(None, ["Evo 2 results", 2, "<?xml version=\"1.0\" ?>\n<bookmark title=\"(evolution) imap filter\" type=\"3\"><search>imap filter</search><product>evolution</product></bookmark>"]) 
    25                 self.listing.insert_before(None, ["many results", 2, "<?xml version=\"1.0\" ?>\n<bookmark title=\"microsoft has majority\" type=\"2\"><search>microsoft has majority</search></bookmark>"]) 
    26                 self.listing.insert_before(None, ["Evo's bugs", 3, "<?xml version=\"1.0\" ?>\n<bookmark title=\"(evolution)\" type=\"3\"><product>evolution</product></bookmark>"]) 
    27                 self.listing.insert_before(None, ["Bug #42", 1, "<?xml version=\"1.0\" ?>\n<bookmark title=\"bug #42\" type=\"1\"><bug>42</bug></bookmark>"]) 
    28                 self.listing.insert_before(None, ["Content in +text mode", 1, "<?xml version=\"1.0\" ?>\n<bookmark title=\"bug #51835\" type=\"1\"><bug>51835</bug></bookmark>"]) 
    29                 self.listing.insert_before(None, ["Search in bugs+text", 1, "<?xml version=\"1.0\" ?>\n<bookmark title=\"bug #51836\" type=\"1\"><bug>51836</bug></bookmark>"]) 
     23                self.listing = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING) 
     24                if os.path.exists(bmark_file) : 
     25                        doc = xml.dom.minidom.parse(bmark_file) 
     26                        for bmark in doc.getElementsByTagName("bookmark") : 
     27                                title = bmark.getAttribute("title") 
     28                                self.listing.insert_before(None, [title, bmark.toxml()]) 
     29                else : 
     30                        doc = xml.dom.minidom.Document() 
     31                        store = doc.createElement("store") 
     32                        doc.appendChild(store) 
     33                        #here we can put hardcoded default 
     34                        # 
     35                        #then we create the file 
     36                        f = open(bmark_file, mode='a+') 
     37                        f.write(doc.toprettyxml()) 
     38                        f.close() 
    3039 
    3140         
     
    3342                return self.listing 
    3443 
    35         def add(self,title,searchMethod, args) : 
     44        def add(self,args) : 
    3645                #TODO : do not forget to also add the bookmarks to 
    3746                #the stored bookmarks. (sync with storage) 
    38                 self.listing.insert_before(None, [title, searchMethod, args]) 
     47                doc2 = xml.dom.minidom.parseString(args) 
     48                bmark = doc2.getElementsByTagName("bookmark")[0] 
     49                title = bmark.getAttribute("title") 
     50                self.listing.insert_before(None, [title, args]) 
     51                self.sync_file() 
    3952 
    4053        def delete(self, to_delete) : 
     
    4255                #the stored bookmarks. (sync with storage) 
    4356                self.listing.remove(to_delete) 
     57                self.sync_file() 
     58 
     59        def sync_file(self) : 
     60                doc = xml.dom.minidom.Document() 
     61                store = doc.createElement("store") 
     62                doc.appendChild(store) 
     63                for i in self.listing : 
     64                        doc2 = xml.dom.minidom.parseString(i[1]) 
     65                        bmark = doc2.getElementsByTagName("bookmark")[0] 
     66                        store.appendChild(bmark) 
     67                #it's maybe not optimal to open/close the file each time we sync 
     68                # but I'm not sure that those operations are so frequent 
     69                # might be changed in the future. 
     70                f = open(bmark_file, mode='w+') 
     71                f.write(doc.toprettyxml()) 
     72                f.close() 
     73                 
    4474 
    4575# this is a lonely bookmark in the store 
     
    5282                title = '' 
    5383                zetuple = path.get_selection().get_selected() 
    54                 method = zetuple[0].get_value(zetuple[1],1) 
    55                 string = zetuple[0].get_value(zetuple[1],2) 
     84                #method = zetuple[0].get_value(zetuple[1],1) 
     85                string = zetuple[0].get_value(zetuple[1],1) 
    5686                doc2 = xml.dom.minidom.parseString(string) 
    5787                bmark = doc2.getElementsByTagName("bookmark")[0] 
    58                 #method =  int(bmark.getAttribute("type")) 
     88                method =  int(bmark.getAttribute("type")) 
    5989                if method == 1: 
    6090                        # besoin de programmation défensive ici FIXME 
     
    73103                        else :   
    74104                                search = s_element[0].childNodes[0].nodeValue 
    75                 self.bug = bug 
     105                self.bug = str(bug) 
    76106                self.method = method 
    77107                self.search = search 
    78108                self.product = product 
    79                 self.title = zetuple[0].get_value(zetuple[1],0) 
     109                self.title = bmark.getAttribute("title") 
    80110 
    81111        def get_method(self) :