Sat, 20 Oct 2012 22:46:24 +0200
Implemented the import functionality.
TimeTracker/TimeTracker.py | file | annotate | diff | comparison | revisions | |
TimeTracker/TimeTrackerWidget.py | file | annotate | diff | comparison | revisions |
--- a/TimeTracker/TimeTracker.py Sat Oct 20 22:01:38 2012 +0200 +++ b/TimeTracker/TimeTracker.py Sat Oct 20 22:46:24 2012 +0200 @@ -145,6 +145,72 @@ """ saved.</p><p>Reason: {1}</p>""").format( self.__trackerFilePath, str(err))) + def importTrackerEntries(self, fname): + """ + Public slot to import tracker entries from a file. + + @param fname name of the file to import (string) + """ + try: + f = open(fname, "r", encoding="utf-8") + data = f.read() + f.close() + except (IOError, OSError) as err: + E5MessageBox.critical(self.__ui, + self.trUtf8("Import Time Tracker File"), + self.trUtf8("""<p>The time tracker file <b>{0}</b> could not be""" + """ read.</p><p>Reason: {1}</p>""").format( + fname, str(err))) + return + + invalidCount = 0 + duplicateCount = 0 + entries = [] + for line in data.splitlines(): + entry = TimeTrackEntry(self.__plugin) + eid = entry.fromString(line.strip()) + if eid > -1: + entries.append(entry) + else: + invalidCount += 1 + + if not self.__plugin.getPreferences("AllowDuplicates"): + startDateTimes = [ + entry.getStartDateTime() for entry in self.__entries.values()] + for entry in entries[:]: + if entry.getStartDateTime() in startDateTimes: + entries.remove(entry) + duplicateCount += 1 + + if len(self.__entries.keys()): + nextID = max(self.__entries.keys()) + 1 + else: + nextID = 0 + for entry in entries: + entry.setID(nextID) + self.__entries[nextID] = entry + nextID += 1 + + if invalidCount != 0 or duplicateCount != 0: + if invalidCount != 0 and duplicateCount != 0: + msg = self.tr("""<p>The time tracker file <b>{0}</b> contained""" + """ %n invalid entries.""", + "", invalidCount).format(fname) + msg += " " + self.tr("""%n duplicate entries were detected.""", + "", duplicateCount) + elif duplicateCount != 0: + msg = self.tr("""<p>The time tracker file <b>{0}</b> contained""" + """ %n duplicate entries.""", + "", duplicateCount).format(fname) + elif invalidCount != 0: + msg = self.tr("""<p>The time tracker file <b>{0}</b> contained""" + """ %n invalid entries.""", + "", invalidCount).format(fname) + msg += " " + self.tr("""These have been ignored.""") + E5MessageBox.information(self.__ui, + self.trUtf8("Import Time Tracker File"), + msg) + def pauseTrackerEntry(self): """ Public method to pause the current tracker entry.
--- a/TimeTracker/TimeTrackerWidget.py Sat Oct 20 22:01:38 2012 +0200 +++ b/TimeTracker/TimeTrackerWidget.py Sat Oct 20 22:46:24 2012 +0200 @@ -7,6 +7,8 @@ Module implementing the time tracker widget. """ +import os + from PyQt4.QtCore import pyqtSlot, QPoint, Qt, QDate, QTime, QFileInfo from PyQt4.QtGui import QWidget, QMenu, QTreeWidgetItem, QCursor @@ -120,7 +122,8 @@ menu.addAction(self.tr("Save"), self.__saveEntries) menu.addSeparator() menu.addAction(self.tr("Import"), self.__importEntries) - menu.addAction(self.tr("Export Selected"), self.__exportSelectedEntries) + menu.addAction(self.tr("Export Selected"), self.__exportSelectedEntries)\ + .setEnabled(len(self.entriesList.selectedItems()) != 0) menu.addAction(self.tr("Export All"), self.__exportEntries) menu.exec_(QCursor.pos()) @@ -158,8 +161,22 @@ """ Private slot to import tracker entries. """ - # TODO: not implemented yet - raise NotImplementedError + path = Preferences.getMultiProject("Workspace") or Utilities.getHomeDir() + fname = E5FileDialog.getOpenFileName( + None, + self.trUtf8("Import Tracker Entries"), + path, + self.trUtf8("Text Files (*.txt);;All Files (*)")) + if fname: + fname = Utilities.toNativeSeparators(fname) + if not os.path.exists(fname): + E5MessageBox.critical(self, + self.trUtf8("Import Tracker Entries"), + self.trUtf8("<p>The file <b>{0}</b> does not exist.</p>").format( + fname)) + return + + self.__tracker.importTrackerEntries(fname) def __exportEntries(self, ids=[]): """