|
From: Stas Z. <sta...@us...> - 2005-06-14 14:22:30
|
Update of /cvsroot/gmailagent/GA-libgmail2/frontend In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26544 Modified Files: Observers.py Added Files: GuiQTgcm.py Qgcm.py Removed Files: GuiQTgmc.py Qgmc.py Log Message: renamed files --- Qgmc.py DELETED --- --- NEW FILE: Qgcm.py --- # -*- coding: latin-1 -*- # Copyright (c) 2005 Stas Zykiewicz <st...@li...> # # Qgmc.py # # This program is free 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 Library 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. import sys from qt import * import GuiQTgmc app = QApplication(sys.argv) w = GuiQTgmc.MainWin(application=app) app.setMainWidget(w) w.show() app.exec_loop() --- NEW FILE: GuiQTgcm.py --- # -*- coding: latin-1 -*- # Copyright (c) 2005 Stas Zykiewicz <st...@li...> # # Contrgmc.py # # This program is free 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 Library 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. CG_DEBUG = 1 import libgmail2,pickle def upload_local_contacts(addrfile,ga): """Returns the contacts from the local file which don't have a ID entry. Contacts without an ID are considered local only and must be added to the Gmail contacts list. This function is called whenever the user logs into Gmail. Returns error message on faillure, None on succes.""" if CG_DEBUG: logging.debug("\n>>>>>>>> Function: upload_local_contacts") contacts = load_addressfile(addrfile) for line in contacts: if CG_DEBUG: logging.debug("Found local contact %s %s %s %s" % line) if not line[3]: logging.debug("Adding %s %s %s %s to Gmail" % line) if not ga.addContact(line[0],line[1],line[2]):#libgmail2 checks for exception return ("Unable to add contact %s %s %s %s" % line) # Now we reload the contacts to create a new local contacts list #import_contacts(addrfile,ga) def store_address(addrfile,name,addr,notes='',id='',ga=None): """The contacts will be stored in the local copy on disk. If the id is given we assume the contact has a valid Gmail id and the contact is removed from the online contacts and replaced by the given contact. It will get a new id from Gmail. """ if CG_DEBUG: logging.debug("\n>>>>>>>> Function: store_address") logging.debug("store contact %s %s %s %s %s" % (name,addr,notes,id,ga)) if id and ga: if CG_DEBUG: logging.debug("Found id, removing original contact first") contacts = ga.getContacts() contact = contacts.getContactById(id) if contact: if not ga.removeContact(contact): logging.warning("Failed to remove contact %s" % contact) else: # Build the new local file with the contacts from Gmail import_contacts(addrfile,ga) # now we add the new contact to the local file store_address(addrfile,name,addr,notes) # And upload it to Gmail upload_local_contacts(addrfile,ga) # Retrieve Gmail contacts so that we have get an id for the new contact. import_contacts(addrfile,ga) # stuff is stored in a file like this: # name&&addr&¬es&&id # Contacts that are not (yet) in the Gmail contacts list have an empty # string as id. # After syncronizing the file with the contacts the id fields will be asigned. try: f = open(addrfile,'r') lines = f.readlines() f.close() except IOError,info: mesg = "Failed to check the address (IO), %s" % info if CG_DEBUG: logging.warning(mesg) return mesg else: for line in lines: if name in line and addr in line: return # It's a new entry line = "%s&&%s&&%s&&%s\n" % (name,addr,notes,id) if CG_DEBUG: logging.debug("Found new contact %s" % line) try: f = open(addrfile,'a') f.write(line) f.close() except IOError,info: f.close() mesg = "Failed to store address, %s" % info if CG_DEBUG: logging.warning(mesg) return mesg def load_addressfile(filename): if CG_DEBUG: logging.debug("\n>>>>>>>> Function: load_addressfile") try: f = open(filename,'r') lines = f.readlines() f.close() except IOError,info: if CG_DEBUG: logging.warning(str(info)) return if CG_DEBUG: logging.debug(str(lines)) newlines = [] for line in lines: newlines.append(tuple(line[:-1].split("&&"))) return (newlines) def import_contacts(addrfile,ga): """This gets the gmail contacts create a new local contacts list on disk.""" if CG_DEBUG: logging.debug("\n>>>>>>>> Function: import_contacts") upload_local_contacts(addrfile,ga) contacts = ga.getContacts().getAllContacts() logging.debug("Contacts imported:\n%s" % contacts) if contacts: # remove local file, we can remove it because the upload_local_contacts method will make sure # that the contacts are syncronized. logging.debug("Removing local file") open(addrfile,'w').close() else: return for obj in contacts: store_address(addrfile,\ obj.getName(),\ obj.getEmail(),\ obj.getNotes(),\ obj.getId()) return load_addressfile(addrfile) def export_contacts(addrfile,ga): """This will remove the contacts at the online contacts list and replace it with the local copy fom disk. It will return a message on any error. It returns None on succes.""" if CG_DEBUG: logging.debug("\n>>>>>>>> Function: export_contacts") entries = load_addressfile(addrfile) if len(entries) < 1: text = "Failed to load address file, %s" % addrfile if CG_DEBUG: logging.critical(text) return text try: contacts = ga.getContacts().getAllContacts() # Just to test save_contacts save_contacts(contacts) for contact in contacts: if CG_DEBUG: logging.debug("Removing contact %s", contact) ga.removeContact(contact) except Exception,info: logging.critical(str(info)) if contacts: save_contacts(contacts) return "An error occured, tried to save your contacts to\n%s and %s" %\ ('~/.GmailContacts.txt','~/.GmailContacts.pickle') # Now we fill the contacts list again from the local file. try: for entry in entries: if CG_DEBUG: logging.debug("Adding contact %s", entry) apply(ga.addContact,entry[:-1]) except Exception,info: logging.critical(str(info)) save_contacts(contacts) def save_contacts(contacts): # This is probably not needed anymore, if shelve turn out OK """Save the contact in case of an error. This is an attempt to prevent data loss in case of an error when interacting with Gmail contacts.""" if CG_DEBUG: logging.debug("\n>>>>>>>> Function: save_contacts") try: f = open(os.path.expanduser('~/.GmailContacts.pickle'),'w') pickle.dump(contacts,f) ## XXX TODO: some way to load the pickled object and restore it except Exception,info: text = "Attempt to pickle the contacts %s" % str(info) logging.critical(text) f.close() try: f =open(os.path.expanduser('~/.GmailContacts.txt'),'w') text = "#An error occured in GmailAgent and these contacts are saved\n"+\ "#to this file in an attempt to save them\n"+\ "#A contact is saved as follows:\n"+\ "#GmailID Name email notes\n" f.write(text) for con in contacts: name,email,notes,id = con.getName(),con.getEmail(),con.getNotes(),con.getId() f.write("%s %s %s %s\n" % (con.getId(),\ con.getName(),\ con.getEmail(),\ con.getNotes())) f.close() except Exception,info: logging.critical(str(info)) return str(info) Index: Observers.py =================================================================== RCS file: /cvsroot/gmailagent/GA-libgmail2/frontend/Observers.py,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Observers.py 12 Jun 2005 06:17:01 -0000 1.1 --- Observers.py 14 Jun 2005 14:22:19 -0000 1.2 *************** *** 69,71 **** print "observable",self cb = getattr(observer,func,None) ! cb(self,data) --- 69,71 ---- print "observable",self cb = getattr(observer,func,None) ! apply(cb,(self,data)) --- GuiQTgmc.py DELETED --- |