From: <cl...@us...> - 2003-03-24 02:39:19
|
Update of /cvsroot/todo-manager/todo-manager In directory sc8-pr-cvs1:/tmp/cvs-serv16881 Modified Files: ChangeLog.txt main.py Log Message: The user will now be told when there is a backup file that can be used to restore changes to a file that was open when the app crashed. A dialog box will also be displayed when the original file is newer than the backup. Index: ChangeLog.txt =================================================================== RCS file: /cvsroot/todo-manager/todo-manager/ChangeLog.txt,v retrieving revision 1.39 retrieving revision 1.40 diff -u -d -r1.39 -r1.40 --- ChangeLog.txt 16 Mar 2003 19:58:20 -0000 1.39 +++ ChangeLog.txt 24 Mar 2003 02:39:12 -0000 1.40 @@ -3,6 +3,8 @@ latest: * Added an option that saves a backup of the current task list in the user's (.)todo-manager directory for crash recovery + - The user will be prompted if they want to restore the original file + from the backup copy Version 0.70.2 (03/16/2003): * Groups with multilingual characters no longer cause an exception Index: main.py =================================================================== RCS file: /cvsroot/todo-manager/todo-manager/main.py,v retrieving revision 1.72 retrieving revision 1.73 diff -u -d -r1.72 -r1.73 --- main.py 11 Mar 2003 03:41:40 -0000 1.72 +++ main.py 24 Mar 2003 02:39:13 -0000 1.73 @@ -120,6 +120,9 @@ self.__current_file = self.__default_file = os.path.join(userdir, 'default.tmf') self.__file_modified = TRUE + # Keep track if a temp file was found + self.__found_tmpfile = self.__locate_tmpfile() + def __create_new_task(self, name=""): task = { "Name": name, @@ -390,8 +393,15 @@ if self.get_setting("Core", "savebackup", FALSE): if not self.__tmpfile: # Generate a temp filename - tmp = "tmp%d.tmf" %int(time.time()) - self.__tmpfile = os.path.join(get_user_directory(), tmp) + dat = os.path.join(get_user_directory(), "tmp%d" %int(time.time())) + self.__tmpfile = "%s.tmf" %dat + + # Write the data file. This stores extra info about the backup + file = open(dat, 'w') + # Write the filename + curfile = ("None\n", "%s\n" %self.__current_file)[self.__current_file != None] + file.write(curfile) + file.close() # Write the file file = open(self.__tmpfile, 'w') @@ -403,8 +413,80 @@ if os.path.isfile(self.__tmpfile): # Remove the file os.remove(self.__tmpfile) + # Remove the data file + os.remove(os.path.splitext(self.__tmpfile)[0]) self.__tmpfile = None + def __locate_tmpfile(self): + # The list that is returned after the scan + # Name of file, Name of backup, recently modified + retlist = [] + + # Get a list of all files in the directory + l = os.listdir(get_user_directory()) + + # This should ask the user if they want to restore all tmp files found + for f in l: + # Find the temp data file + if f[:3] == 'tmp' and not os.path.splitext(f)[1]: + # Get the name of the file + tmpfile = os.path.join(get_user_directory(), f) + file = open(tmpfile, 'r') + # Read the source filename from this file + origfile = file.read().strip() + file.close() + + # Make sure the files still exists + tmpfile = tmpfile + '.tmf' # add the extension + if os.path.isfile(origfile) and os.path.isfile(tmpfile): + # Compare modification times + # If the original has been modified then don't prompt for a restore + ofm = os.path.getmtime(origfile) + bfm = os.path.getmtime(tmpfile) + + # Add the items to the list + retlist.append([origfile, tmpfile, bfm > ofm]) + + return retlist + + def __prompt_tmpfile(self, ui): + for f in self.__found_tmpfile: + i = 0 + if f[2]: + i = self.interface_call(ui, "ask_yes_no", _("Restore Backup File"), + _("A backup of the file %s was found. Would you like to recover all "\ + "changes for this file?") %f[0]) + + # If the user responded "yes" then restore the file + if i: self.__restore_tmpfile(f[1], f[0]) + + else: + i = not self.interface_call(ui, "ask_yes_no", _("Backup File Warning"), + _("A backup file for %s was found at %s but cannot be restored because "\ + "the original file has been modified since the backup file was created. "\ + "would you like to preserve the backup file anyway?") %(f[0], f[1])) + + # Delete the tmp files + if i: + os.remove(os.path.splitext(f[1])[0]) + os.remove(f[1]) + + # Set this back to a blank list so it isn't given to other ui's + self.__found_tmpfile = [] + + def __restore_tmpfile(self, tmpfile, origfile): + # Read the backup + file = open(tmpfile, 'r') + self.__load_file(file) + file.close() + + # Write to the original + file = open(origfile, 'w') + self.__save_file(self.__dup_task_list(), file) + file.close() + + self.__current_file = origfile + def do_file_open(self, filename=None, ui=None): self.__ext_load_file(filename=filename, ui=ui) @@ -601,6 +683,9 @@ if not interface in self.__interfaces: self.__interfaces.append(interface) + + # If a temp file was found then tell the first interface + self.__prompt_tmpfile(interface) # Tell the interface a few things it should know |