File: iocontroller.py

package info (click to toggle)
taskcoach 1.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 32,188 kB
  • sloc: python: 72,373; makefile: 285; ansic: 120; xml: 34; sh: 16
file content (517 lines) | stat: -rw-r--r-- 23,509 bytes parent folder | download
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# -*- coding: utf-8 -*-

'''
Task Coach - Your friendly task manager
Copyright (C) 2004-2016 Task Coach developers <developers@taskcoach.org>

Task Coach 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 3 of the License, or
(at your option) any later version.

Task Coach 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, see <http://www.gnu.org/licenses/>.
'''

from taskcoachlib import meta, persistence, patterns, operating_system
from taskcoachlib.i18n import _
import lockfile
from taskcoachlib.widgets import GetPassword
from taskcoachlib.workarounds import ExceptionAsUnicode
from taskcoachlib.gui.dialog import BackupManagerDialog
import wx
import os
import gc
import sys
import codecs
import traceback

try:
    from taskcoachlib.syncml import sync
except ImportError:  # pragma: no cover
    # Unsupported platform.
    pass


class IOController(object): 
    ''' IOController is responsible for opening, closing, loading,
    saving, and exporting files. It also presents the necessary dialogs
    to let the user specify what file to load/save/etc. '''

    def __init__(self, taskFile, messageCallback, settings, splash=None): 
        super(IOController, self).__init__()
        self.__taskFile = taskFile
        self.__messageCallback = messageCallback
        self.__settings = settings
        self.__splash = splash
        defaultPath = os.path.expanduser('~')
        self.__tskFileSaveDialogOpts = {'default_path': defaultPath, 
            'default_extension': 'tsk', 'wildcard': 
            _('%s files (*.tsk)|*.tsk|All files (*.*)|*') % meta.name}
        self.__tskFileOpenDialogOpts = {'default_path': defaultPath, 
            'default_extension': 'tsk', 'wildcard': 
            _('%s files (*.tsk)|*.tsk|Backup files (*.tsk.bak)|*.tsk.bak|'
              'All files (*.*)|*') % meta.name}
        self.__icsFileDialogOpts = {'default_path': defaultPath, 
            'default_extension': 'ics', 'wildcard': 
            _('iCalendar files (*.ics)|*.ics|All files (*.*)|*')}
        self.__htmlFileDialogOpts = {'default_path': defaultPath, 
            'default_extension': 'html', 'wildcard': 
            _('HTML files (*.html)|*.html|All files (*.*)|*')}
        self.__csvFileDialogOpts = {'default_path': defaultPath,
            'default_extension': 'csv', 'wildcard': 
            _('CSV files (*.csv)|*.csv|Text files (*.txt)|*.txt|'
              'All files (*.*)|*')}
        self.__todotxtFileDialogOpts = {'default_path': defaultPath,
            'default_extension': 'txt', 'wildcard':
            _('Todo.txt files (*.txt)|*.txt|All files (*.*)|*')}
        self.__errorMessageOptions = dict(caption=_('%s file error') % \
                                          meta.name, style=wx.ICON_ERROR)

    def syncMLConfig(self):
        return self.__taskFile.syncMLConfig()

    def setSyncMLConfig(self, config):
        self.__taskFile.setSyncMLConfig(config)

    def needSave(self):
        return self.__taskFile.needSave()

    def changedOnDisk(self):
        return self.__taskFile.changedOnDisk()

    def hasDeletedItems(self):
        return bool([task for task in self.__taskFile.tasks() if task.isDeleted()] + \
                    [note for note in self.__taskFile.notes() if note.isDeleted()])

    def purgeDeletedItems(self):
        self.__taskFile.tasks().removeItems([task for task in self.__taskFile.tasks() if task.isDeleted()])
        self.__taskFile.notes().removeItems([note for note in self.__taskFile.notes() if note.isDeleted()])

    def openAfterStart(self, commandLineArgs):
        ''' Open either the file specified on the command line, or the file
            the user was working on previously, or none at all. '''
        if commandLineArgs:
            filename = commandLineArgs[0].decode(sys.getfilesystemencoding())
        else:
            filename = self.__settings.get('file', 'lastfile')
        if filename:
            # Use CallAfter so that the main window is opened first and any 
            # error messages are shown on top of it
            wx.CallAfter(self.open, filename)
            
    def open(self, filename=None, showerror=wx.MessageBox, 
             fileExists=os.path.exists, breakLock=False, lock=True):
        if self.__taskFile.needSave():
            if not self.__saveUnsavedChanges():
                return
        if not filename:
            filename = self.__askUserForFile(_('Open'), 
                                             self.__tskFileOpenDialogOpts)
        if not filename:
            return
        self.__updateDefaultPath(filename)
        if fileExists(filename):
            self.__closeUnconditionally()
            self.__addRecentFile(filename)
            try:
                try:
                    self.__taskFile.load(filename, lock=lock, 
                                         breakLock=breakLock)
                except:
                    # Need to destroy splash screen first because it may 
                    # interfere with dialogs we show later on Mac OS X
                    if self.__splash:
                        self.__splash.Destroy()
                    raise
            except lockfile.LockTimeout:
                if breakLock:
                    if self.__askOpenUnlocked(filename): 
                        self.open(filename, showerror, lock=False)
                elif self.__askBreakLock(filename):
                    self.open(filename, showerror, breakLock=True)
                else:
                    return
            except lockfile.LockFailed:
                if self.__askOpenUnlocked(filename): 
                    self.open(filename, showerror, lock=False)
                else:
                    return
            except persistence.xml.reader.XMLReaderTooNewException:
                self.__showTooNewErrorMessage(filename, showerror)
                return
            except Exception:
                self.__showGenericErrorMessage(filename, showerror, showBackups=True)
                return
            self.__messageCallback(_('Loaded %(nrtasks)d tasks from '
                 '%(filename)s') % dict(nrtasks=len(self.__taskFile.tasks()), 
                                        filename=self.__taskFile.filename()))
        else:
            errorMessage = _("Cannot open %s because it doesn't exist") % filename
            # Use CallAfter on Mac OS X because otherwise the app will hang:
            if operating_system.isMac():
                wx.CallAfter(showerror, errorMessage, **self.__errorMessageOptions)
            else:
                showerror(errorMessage, **self.__errorMessageOptions)
            self.__removeRecentFile(filename)
            
    def merge(self, filename=None, showerror=wx.MessageBox):
        if not filename:
            filename = self.__askUserForFile(_('Merge'), 
                                             self.__tskFileOpenDialogOpts)
        if filename:
            try:
                self.__taskFile.merge(filename)
            except lockfile.LockTimeout:
                showerror(_('Cannot open %(filename)s\nbecause it is locked.') % \
                          dict(filename=filename),
                          **self.__errorMessageOptions)
                return
            except persistence.xml.reader.XMLReaderTooNewException:
                self.__showTooNewErrorMessage(filename, showerror)
                return
            except Exception:
                self.__showGenericErrorMessage(filename, showerror)
                return
            self.__messageCallback(_('Merged %(filename)s') % \
                                   dict(filename=filename))
            self.__addRecentFile(filename)

    def save(self, showerror=wx.MessageBox):
        if self.__taskFile.filename():
            if self._saveSave(self.__taskFile, showerror):
                return True
            else:
                return self.saveas(showerror=showerror)
        elif not self.__taskFile.isEmpty():
            return self.saveas(showerror=showerror)  # Ask for filename
        else:
            return False

    def mergeDiskChanges(self):
        self.__taskFile.mergeDiskChanges()

    def saveas(self, filename=None, showerror=wx.MessageBox, 
               fileExists=os.path.exists):
        if not filename:
            filename = self.__askUserForFile(_('Save as'), 
                self.__tskFileSaveDialogOpts,
                flag=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT, fileExists=fileExists)
            if not filename:
                return False  # User didn't enter a filename, cancel save
        if self._saveSave(self.__taskFile, showerror, filename):
            return True
        else:
            return self.saveas(showerror=showerror)  # Try again

    def saveselection(self, tasks, filename=None, showerror=wx.MessageBox,
                      TaskFileClass=persistence.TaskFile, 
                      fileExists=os.path.exists):
        if not filename:
            filename = self.__askUserForFile(_('Save selection'),
                self.__tskFileSaveDialogOpts, 
                flag=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT, fileExists=fileExists)
            if not filename:
                return False  # User didn't enter a filename, cancel save
        selectionFile = self._createSelectionFile(tasks, TaskFileClass)
        if self._saveSave(selectionFile, showerror, filename):
            return True
        else:
            return self.saveselection(tasks, showerror=showerror, 
                                      TaskFileClass=TaskFileClass)  # Try again
            
    def _createSelectionFile(self, tasks, TaskFileClass):
        selectionFile = TaskFileClass()
        # Add the selected tasks:
        selectionFile.tasks().extend(tasks)
        # Include categories used by the selected tasks:
        allCategories = set()
        for task in tasks:
            allCategories.update(task.categories())
        # Also include parents of used categories, recursively:
        for category in allCategories.copy():
            allCategories.update(category.ancestors())
        selectionFile.categories().extend(allCategories)
        return selectionFile
    
    def _saveSave(self, taskFile, showerror, filename=None):
        ''' Save the file and show an error message if saving fails. '''
        try:
            if filename:
                taskFile.saveas(filename)
            else:
                filename = taskFile.filename()
                taskFile.save()
            self.__showSaveMessage(taskFile)
            self.__addRecentFile(filename)
            return True
        except lockfile.LockTimeout:
            errorMessage = _('Cannot save %s\nIt is locked by another instance '
                             'of %s.\n') % (filename, meta.name)
            showerror(errorMessage, **self.__errorMessageOptions)
            return False
        except (OSError, IOError, lockfile.LockFailed), reason:
            errorMessage = _('Cannot save %s\n%s') % (filename, 
                           ExceptionAsUnicode(reason))
            showerror(errorMessage, **self.__errorMessageOptions)
            return False
        
    def saveastemplate(self, task):
        templates = persistence.TemplateList(self.__settings.pathToTemplatesDir())
        templates.addTemplate(task)
        templates.save()

    def importTemplate(self, showerror=wx.MessageBox):
        filename = self.__askUserForFile(_('Import template'),
            fileDialogOpts={'default_extension': 'tsktmpl', 
                            'wildcard': _('%s template files (*.tsktmpl)|'
                                          '*.tsktmpl') % meta.name})
        if filename:
            templates = persistence.TemplateList(self.__settings.pathToTemplatesDir())
            try:
                templates.copyTemplate(filename)
            except Exception, reason:  # pylint: disable=W0703
                errorMessage = _('Cannot import template %s\n%s') % (filename, 
                               ExceptionAsUnicode(reason))
                showerror(errorMessage, **self.__errorMessageOptions)
            
    def close(self, force=False):
        if self.__taskFile.needSave():
            if force:
                # No user interaction, since we're forced to close right now.
                if self.__taskFile.filename():
                    self._saveSave(self.__taskFile, 
                                   lambda *args, **kwargs: None)
                else:
                    pass  # No filename, we cannot ask, give up...
            else:
                if not self.__saveUnsavedChanges():
                    return False
        self.__closeUnconditionally()
        return True
    
    def export(self, title, fileDialogOpts, writerClass, viewer, selectionOnly, 
               openfile=codecs.open, showerror=wx.MessageBox, filename=None, 
               fileExists=os.path.exists, **kwargs):
        filename = filename or self.__askUserForFile(title, fileDialogOpts, 
            flag=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT, fileExists=fileExists)
        if filename:
            fd = self.__openFileForWriting(filename, openfile, showerror)
            if fd is None:
                return False
            count = writerClass(fd, filename).write(viewer, self.__settings, 
                                                    selectionOnly, **kwargs)
            fd.close()
            self.__messageCallback(_('Exported %(count)d items to '
                                     '%(filename)s') % dict(count=count, 
                                                            filename=filename))
            return True
        else:
            return False

    def exportAsHTML(self, viewer, selectionOnly=False, separateCSS=False,
                     columns=None, openfile=codecs.open, 
                     showerror=wx.MessageBox, filename=None, 
                     fileExists=os.path.exists):
        return self.export(_('Export as HTML'), self.__htmlFileDialogOpts, 
            persistence.HTMLWriter, viewer, selectionOnly, openfile, showerror, 
            filename, fileExists, separateCSS=separateCSS, columns=columns)

    def exportAsCSV(self, viewer, selectionOnly=False, 
                    separateDateAndTimeColumns=False, columns=None,
                    fileExists=os.path.exists):
        return self.export(_('Export as CSV'), self.__csvFileDialogOpts, 
            persistence.CSVWriter, viewer, selectionOnly, 
            separateDateAndTimeColumns=separateDateAndTimeColumns, 
            columns=columns, fileExists=fileExists)
        
    def exportAsICalendar(self, viewer, selectionOnly=False, 
                          fileExists=os.path.exists):
        return self.export(_('Export as iCalendar'),
            self.__icsFileDialogOpts, persistence.iCalendarWriter, viewer, 
            selectionOnly, fileExists=fileExists)
        
    def exportAsTodoTxt(self, viewer, selectionOnly=False,
                        fileExists=os.path.exists):
        return self.export(_('Export as Todo.txt'),
            self.__todotxtFileDialogOpts, persistence.TodoTxtWriter, viewer,
            selectionOnly, fileExists=fileExists)

    def importCSV(self, **kwargs):
        persistence.CSVReader(self.__taskFile.tasks(),
                              self.__taskFile.categories()).read(**kwargs)
                              
    def importTodoTxt(self, filename):
        persistence.TodoTxtReader(self.__taskFile.tasks(),
                                  self.__taskFile.categories()).read(filename)

    def synchronize(self):
        doReset = False
        while True:
            password = GetPassword('Task Coach', 'SyncML', reset=doReset)
            if not password:
                break

            synchronizer = sync.Synchronizer(self.__syncReport,
                                         self.__taskFile, password)
            try:
                synchronizer.synchronize()
            except sync.AuthenticationFailure:
                doReset = True
            else:
                self.__messageCallback(_('Finished synchronization'))
                break
            finally:
                synchronizer.Destroy()

    def filename(self):
        return self.__taskFile.filename()

    def __syncReport(self, msg):
        wx.MessageBox(msg, _('Synchronization status'), 
                      style=wx.OK | wx.ICON_ERROR)

    def __openFileForWriting(self, filename, openfile, showerror, mode='w', 
                             encoding='utf-8'):
        try:
            return openfile(filename, mode, encoding)
        except IOError, reason:
            errorMessage = _('Cannot open %s\n%s') % (filename, 
                           ExceptionAsUnicode(reason))
            showerror(errorMessage, **self.__errorMessageOptions)
            return None
        
    def __addRecentFile(self, fileName):
        recentFiles = self.__settings.getlist('file', 'recentfiles')
        if fileName in recentFiles:
            recentFiles.remove(fileName)
        recentFiles.insert(0, fileName)
        maximumNumberOfRecentFiles = self.__settings.getint('file', 
                                                            'maxrecentfiles')
        recentFiles = recentFiles[:maximumNumberOfRecentFiles]
        self.__settings.setlist('file', 'recentfiles', recentFiles)
        
    def __removeRecentFile(self, fileName):
        recentFiles = self.__settings.getlist('file', 'recentfiles')
        if fileName in recentFiles:
            recentFiles.remove(fileName)
            self.__settings.setlist('file', 'recentfiles', recentFiles)
        
    def __askUserForFile(self, title, fileDialogOpts, flag=wx.FD_OPEN, 
                         fileExists=os.path.exists):
        filename = wx.FileSelector(title, flags=flag, 
                                   **fileDialogOpts)  # pylint: disable=W0142
        if filename and (flag & wx.FD_SAVE):
            # On Ubuntu, the default extension is not added automatically to
            # a filename typed by the user. Add the extension if necessary.
            extension = os.path.extsep + fileDialogOpts['default_extension']
            if not filename.endswith(extension):
                filename += extension
                if fileExists(filename):
                    return self.__askUserForOverwriteConfirmation(filename, title, 
                                                                  fileDialogOpts)
        return filename
    
    def __askUserForOverwriteConfirmation(self, filename, title, 
                                          fileDialogOpts):
        result = wx.MessageBox(_('A file named %s already exists.\n'
                                 'Do you want to replace it?') % filename,
            title, 
            style=wx.YES_NO | wx.CANCEL | wx.ICON_QUESTION | wx.NO_DEFAULT)
        if result == wx.YES:
            extensions = { 'Todo.txt': '.txt' }
            for auto in set(self.__settings.getlist('file', 'autoimport') + self.__settings.getlist('file', 'autoexport')):
                autoName = os.path.splitext(filename)[0] + extensions[auto]
                if os.path.exists(autoName):
                    os.remove(autoName)
                if os.path.exists(autoName + '-meta'):
                    os.remove(autoName + '-meta')
            return filename
        elif result == wx.NO:
            return self.__askUserForFile(title, fileDialogOpts, 
                                         flag=wx.FD_SAVE | \
                                              wx.FD_OVERWRITE_PROMPT)
        else:
            return None

    def __saveUnsavedChanges(self):
        result = wx.MessageBox(_('You have unsaved changes.\n'
            'Save before closing?'), _('%s: save changes?') % meta.name,
            style=wx.YES_NO | wx.CANCEL | wx.ICON_QUESTION | wx.YES_DEFAULT)
        if result == wx.YES:
            if not self.save():
                return False
        elif result == wx.CANCEL:
            return False
        return True
    
    def __askBreakLock(self, filename):
        result = wx.MessageBox(_('''Cannot open %s because it is locked.

This means either that another instance of TaskCoach
is running and has this file opened, or that a previous
instance of Task Coach crashed. If no other instance is
running, you can safely break the lock.

Break the lock?''') % filename,
            _('%s: file locked') % meta.name,
            style=wx.YES_NO | wx.ICON_QUESTION | wx.NO_DEFAULT)
        return result == wx.YES
    
    def __askOpenUnlocked(self, filename):
        result = wx.MessageBox(_('Cannot acquire a lock because locking is not '
            'supported\non the location of %s.\n'
            'Open %s unlocked?') % (filename, filename), 
             _('%s: file locked') % meta.name,
            style=wx.YES_NO | wx.ICON_QUESTION | wx.NO_DEFAULT)
        return result == wx.YES
    
    def __closeUnconditionally(self):
        self.__messageCallback(_('Closed %s') % self.__taskFile.filename())
        self.__taskFile.close()
        patterns.CommandHistory().clear()
        gc.collect()

    def __showSaveMessage(self, savedFile):    
        self.__messageCallback(_('Saved %(nrtasks)d tasks to %(filename)s') % \
            {'nrtasks': len(savedFile.tasks()), 
             'filename': savedFile.filename()})
        
    def __showTooNewErrorMessage(self, filename, showerror):
        showerror(_('Cannot open %(filename)s\n'
                    'because it was created by a newer version of %(name)s.\n'
                    'Please upgrade %(name)s.') % \
            dict(filename=filename, name=meta.name),
            **self.__errorMessageOptions)
        
    def __showGenericErrorMessage(self, filename, showerror, showBackups=False):
        sys.stderr.write(''.join(traceback.format_exception(*sys.exc_info())))
        limitedException = ''.join(traceback.format_exception(*sys.exc_info(), 
                                                              limit=10))
        message = _('Error while reading %s:\n') % filename + \
                    limitedException
        man = persistence.BackupManifest(self.__settings)
        if showBackups and man.hasBackups(filename):
            message += u'\n' + _('The backup manager will now open to allow you to restore\nan older version of this file.')
        showerror(message, **self.__errorMessageOptions)

        if showBackups and man.hasBackups(filename):
            dlg = BackupManagerDialog(None, self.__settings, filename)
            try:
                if dlg.ShowModal() == wx.ID_OK:
                    wx.CallAfter(self.open, dlg.restoredFilename())
            finally:
                dlg.Destroy()

    def __updateDefaultPath(self, filename):
        for options in [self.__tskFileOpenDialogOpts, 
                        self.__tskFileSaveDialogOpts,
                        self.__csvFileDialogOpts,
                        self.__icsFileDialogOpts, 
                        self.__htmlFileDialogOpts]:
            options['default_path'] = os.path.dirname(filename)