e236451b7295ad010596034fa794e38b9ef951de
[biaweb_qt.git] / generate_dialog.py
1 # BiaWeb Website content manager (c) 2010 V.Harishankar
2 # Generate site dialog
3
4 import PyQt4
5 import sys
6 import os
7 import os.path
8 import ui_generate_dialog
9 import biaweb_exporter
10
11 class GenerateDialog (PyQt4.QtGui.QDialog, ui_generate_dialog.Ui_SiteGenerateDialog):
12 def __init__ (self, master, currentdb):
13 PyQt4.QtGui.QDialog.__init__ (self, master)
14 self.setupUi (self)
15
16 # update the status of required additional files to be copied to
17 # the destination directory and warn the user accordingly
18 self.set_required_files_status ()
19
20 # set the database
21 self.current_db = currentdb
22
23 # function to set the status message of the required additional files to be copied to
24 # the destination directory. If any required additional file is missing in the script
25 # path then warn the user to copy those files manually to the destination directory
26 def set_required_files_status (self):
27 search_script_path = os.path.join (sys.path[0], "search.py")
28 star_image_path = os.path.join (sys.path[0], "star.gif")
29 stargrey_image_path = os.path.join (sys.path[0], "star-grey.gif")
30
31 # if search.py exists in script directory
32 if os.path.exists (search_script_path):
33 self.status_search_py.setText ('<span style="color:darkgreen;">will be \
34 automatically copied to destination dir</span>')
35 else:
36 self.status_search_py.setText ('<span style="color:darkred;">cannot be found.\
37 You must copy it manually to cgi-bin</span>')
38
39 # if star.gif exists in script directory
40 if os.path.exists (star_image_path):
41 self.status_star_gif.setText ('<span style="color:darkgreen;">will be \
42 automatically copied to destination dir</span>')
43 else:
44 self.status_star_gif.setText ('<span style="color:darkred;">Cannot be found. \
45 You must copy it manually to destination dir</span')
46
47 # if star-grey.gif exists in script directory
48 if os.path.exists (stargrey_image_path):
49 self.status_stargrey_gif.setText ('<span style="color:darkgreen;">will be \
50 automatically copied to destination dir</span>')
51 else:
52 self.status_stargrey_gif.setText ('<span style="color:darkred;">cannot be found. \
53 You must copy it manually to destination dir</span')
54
55 # to return the list of items from a tree widget
56 def get_list_from_tree (self, treewidget):
57 lstfiles = []
58 iter = PyQt4.QtGui.QTreeWidgetItemIterator (treewidget)
59
60 # iterate through the list of files
61 item = iter.value ()
62 # while there is still a valid item
63 while item:
64 # get the text in columns 0 and 1
65 src = str (item.text (0))
66 dst = str (item.text (1))
67 # add it to the list
68 lstfiles.append ( (src, dst) )
69 # increase iterator by 1
70 iter += 1
71 item = iter.value ()
72
73 return lstfiles
74
75 # when site generate button is clicked
76 def onSiteGenerate (self):
77 files_list = self.get_list_from_tree (self.additional_files)
78 folder_list = self.get_list_from_tree (self.additional_folders)
79 if self.fulltextindex.isChecked ():
80 searchtype = True
81 else:
82 searchtype = False
83
84 # confirm whether to delete the destination tree and work afresh
85 ans = PyQt4.QtGui.QMessageBox.question (self, "Confirm",
86 "This will delete the destination tree completely \
87 and recreate the website. Are you sure you wish to proceed?",
88 PyQt4.QtGui.QMessageBox.Yes, PyQt4.QtGui.QMessageBox.No)
89
90 # if confirmed
91 if ans == PyQt4.QtGui.QMessageBox.Yes:
92 # call the main exporter to do our work
93 ret = biaweb_exporter.generate_site (self.current_db, files_list, folder_list, searchtype)
94
95 # if failed to generate site or any part thereof
96 if ret == False:
97 PyQt4.QtGui.QMessageBox.critical (self, "Error",
98 "System or SQLite 3 error in generating website or parts thereof")
99 else:
100 PyQt4.QtGui.QMessageBox.information (self, "Success",
101 "Successfully generated website in destination path!")
102
103 # when folder add is clicked
104 def onFolderAdd (self):
105 folder_to_add = PyQt4.QtGui.QFileDialog.getExistingDirectory (self,
106 "Folder whose contents to add to destination", os.path.expanduser ("~"))
107 # if cancelled return
108 if not folder_to_add:
109 return
110
111 dest_rel_path, ok = PyQt4.QtGui.QInputDialog.getText (self, "Desination",
112 "Copy to location (within destination path)")
113 if not ok:
114 return
115
116 folderitem = PyQt4.QtGui.QTreeWidgetItem ([folder_to_add, dest_rel_path])
117 self.additional_folders.addTopLevelItem (folderitem)
118
119 # when folder remove is clicked
120 def onFolderRemove (self):
121 selitem = self.additional_folders.currentItem ()
122 selindex = self.additional_folders.indexOfTopLevelItem (selitem)
123 # if none selected
124 if selindex == -1:
125 return
126 self.additional_folders.takeTopLevelItem (selindex)
127
128
129 # when file add is clicked
130 def onFileAdd (self):
131 file_to_add = PyQt4.QtGui.QFileDialog.getOpenFileName (self, "File to add to destination",
132 os.path.expanduser ("~"))
133 # if cancelled return
134 if not file_to_add:
135 return
136 dest_rel_path, ok = PyQt4.QtGui.QInputDialog.getText (self, "Destination",
137 "Copy to location (within destination path)")
138 if not ok:
139 return
140
141 fileitem = PyQt4.QtGui.QTreeWidgetItem ([file_to_add, dest_rel_path])
142 self.additional_files.addTopLevelItem (fileitem)
143
144 # when file remove is clicked
145 def onFileRemove (self):
146 selitem = self.additional_files.currentItem ()
147 selindex = self.additional_files.indexOfTopLevelItem (selitem)
148 # if none selected
149 if selindex == -1:
150 return
151 self.additional_files.takeTopLevelItem (selindex)