Debian Codegraph @

src/plugins/scripting/scripts/resourcesimport.py

raw
1 #!/usr/bin/env kross
2 # -*- coding: utf-8 -*-
3
4 import os, datetime, sys, traceback, pickle
5 import Kross, Plan
6
7 T = Kross.module("kdetranslation")
8
9 class ResourcesImporter:
10
11 def __init__(self, scriptaction):
12 self.scriptaction = scriptaction
13
14 self.proj = Plan.project()
15
16 self.forms = Kross.module("forms")
17 self.dialog = self.forms.createDialog(T.i18n("Resources Import"))
18 self.dialog.setButtons("Ok|Cancel")
19 self.dialog.setFaceType("List") #Auto Plain List Tree Tabbed
20
21 #TODO add options page ( import Calendars? Select calendars, Select resources... )
22
23 openpage = self.dialog.addPage(T.i18n("Open"),T.i18n("Plan Resources"),"document-open")
24 self.openwidget = self.forms.createFileWidget(openpage, "kfiledialog:///kplatresourcesimportopen")
25 self.openwidget.setMode("Opening")
26 self.openwidget.setFilter("*.plan|%(1)s\n*|%(2)s" % { '1' : T.i18n("Plan"), '2' : T.i18n("All Files") } )
27
28 if self.dialog.exec_loop():
29 try:
30 Plan.beginCommand( T.i18nc( "(qtundo_format )", "Import resources" ) )
31 self.doImport( self.proj )
32 Plan.endCommand()
33 except:
34 Plan.revertCommand() # play safe in case parts where loaded
35 self.forms.showMessageBox("Error", T.i18n("Error"), "%s" % "".join( traceback.format_exception(sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]) ))
36
37 def doImport( self, project ):
38 filename = self.openwidget.selectedFile()
39 if not os.path.isfile(filename):
40 raise Exception, T.i18n("No file selected")
41
42 Other = Plan.openDocument("Other", filename)
43 if Other is None:
44 self.forms.showMessageBox("Sorry", T.i18n("Error"), T.i18n("Could not open document: %1", [filename]))
45 return
46 otherproj = Other.project()
47 if otherproj is None:
48 self.forms.showMessageBox("Sorry", T.i18n("Error"), T.i18n("No project to import from"))
49 return
50 if project.id() == otherproj.id():
51 self.forms.showMessageBox("Sorry", T.i18n("Error"), T.i18n("Project identities are identical"))
52 return
53
54 for ci in range( otherproj.calendarCount() ):
55 self.doImportCalendar( project, otherproj.calendarAt( ci ) )
56
57 defcal = otherproj.defaultCalendar()
58 if defcal is not None:
59 dc = project.findCalendar( defcal.id() )
60 if dc is not None:
61 project.setDefaultCalendar( dc )
62
63 for gi in range( otherproj.resourceGroupCount() ):
64 othergroup = otherproj.resourceGroupAt( gi )
65 gr = project.findResourceGroup( othergroup.id() )
66 if gr is None:
67 gr = project.createResourceGroup( othergroup )
68 if gr is None:
69 self.forms.showMessageBox("Sorry", T.i18n("Error"), T.i18n("Unable to create copy of resource group: %1", [othergroup.name()]))
70 return
71 for ri in range( othergroup.resourceCount() ):
72 otherresource = othergroup.resourceAt( ri )
73 if otherresource is None:
74 self.forms.showMessageBox("Sorry", T.i18n("Error"), T.i18n("No resource to copy from"))
75 return
76 self.doImportResource( project, gr, otherresource )
77
78 def doImportResource( self, project, group, resource ):
79 r = project.findResource( resource.id() )
80 if r is None:
81 r = project.createResource( group, resource )
82 if r is None:
83 self.forms.showMessageBox("Sorry", T.i18n("Error"), T.i18n("Unable to create copy of resource: %1", [resource.name()]))
84 return
85 else:
86 #TODO update?
87 print "Resource already exists: %s %s" % ( r.id(), project.data( r, 'ResourceName' ) )
88
89 def doImportCalendar( self, project, calendar, parent = None ):
90 cal = project.findCalendar( calendar.id() )
91 if cal is not None:
92 #TODO let user decide
93 self.forms.showMessageBox("Sorry", T.i18n("Error"), T.i18n("Calendar already exists: %1", [cal.name()]))
94 return
95 cal = project.createCalendar( calendar, parent )
96 if cal is None:
97 self.forms.showMessageBox("Sorry", T.i18n("Error"), T.i18n("Unable to create copy of calendar: %1", [calendar.name()]))
98 return
99 for ci in range( calendar.childCount() ):
100 self.doImportCalendar( project, calendar.childAt( ci ), cal )
101
102 ResourcesImporter( self )