Refactor to use new protocol module and sipo.
[bertos.git] / wizard / BVersionPage.py
1 #!/usr/bin/env python
2 # encoding: utf-8
3 #
4 # This file is part of BeRTOS.
5 #
6 # Bertos is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19 #
20 # As a special exception, you may use this file as part of a free software
21 # library without restriction.  Specifically, if other files instantiate
22 # templates or use macros or inline functions from this file, or you compile
23 # this file and link it with other files to produce an executable, this
24 # file does not by itself cause the resulting executable to be covered by
25 # the GNU General Public License.  This exception does not however
26 # invalidate any other reasons why the executable file might be covered by
27 # the GNU General Public License.
28 #
29 # Copyright 2008 Develer S.r.l. (http://www.develer.com/)
30 #
31 #
32 # Author: Lorenzo Berni <duplo@develer.com>
33 #
34
35 import os
36
37 from PyQt4.QtGui import *
38 from BWizardPage import *
39 import bertos_utils
40 import qvariant_converter
41
42 from const import *
43
44 class BVersionPage(BWizardPage):
45     """
46     Page of the wizard that permits to choose which BeRTOS version the user wants
47     to use. This page show some pieces of information about the version.
48     """
49     
50     def __init__(self, edit=False):
51         self._edit = edit
52         BWizardPage.__init__(self, UI_LOCATION + "/bertos_versions.ui")
53         self.setTitle(self.tr("Select BeRTOS version"))
54         self.setSubTitle(self.tr("Your project will be created with the specified BeRTOS version"))
55
56     ## Overloaded QWizardPage methods ##
57     
58     def isComplete(self):
59         """
60         Overload of the QWizardPage isComplete method.
61         """
62         if self.pageContent.versionList.currentRow() != -1:
63             sources_path = qvariant_converter.getString(self.pageContent.versionList.currentItem().data(Qt.UserRole))
64             # Remove the trailing slash
65             if sources_path.endswith(os.sep):
66                 sources_path = sources_path[:-1]
67             self.setProjectInfo("BERTOS_PATH", sources_path)
68             return True
69         else:
70             return False
71     
72     ####
73     
74     ## Overloaded BWizardPage methods ##
75
76     def connectSignals(self):
77         """
78         Overload of the BWizardPage connectSignals method.
79         """
80         self.connect(self.pageContent.versionList, SIGNAL("currentItemChanged(QListWidgetItem *, QListWidgetItem*)"), self.rowChanged)
81         self.connect(self.pageContent.addButton, SIGNAL("clicked()"), self.addVersion)
82         self.connect(self.pageContent.removeButton, SIGNAL("clicked()"), self.removeVersion)
83         # Fake signal connection for the update button
84         self.connect(self.pageContent.updateButton, SIGNAL("clicked()"), self.updateClicked)
85     
86     def reloadData(self, previous_id=None):
87         """
88         Overload of the BWizardPage reloadData method.
89         """
90         self.resetVersionList()
91         self.pageContent.versionList.setCurrentRow(-1)
92         self.fillVersionList()
93     
94     def setupUi(self):
95         """
96         Overload of the BWizardPage setupUi method.
97         """
98         self.pageContent.updateButton.setVisible(False)
99     
100     ####
101     
102     ## Slots ##
103     
104     def addVersion(self):
105         """
106         Slot called when the user add a BeRTOS version.
107         """
108         directory = QFileDialog.getExistingDirectory(self, self.tr("Choose a directory"), "", QFileDialog.ShowDirsOnly | QFileDialog.DontResolveSymlinks)
109         if not directory.isEmpty():
110             self.storeVersion(unicode(directory))
111             self.pageContent.versionList.clear()
112             self.fillVersionList()
113             self.emit(SIGNAL("completeChanged()"))
114     
115     def removeVersion(self):
116         """
117         Slot called when the user remove a BeRTOS version.
118         """
119         item = self.pageContent.versionList.takeItem(self.pageContent.versionList.currentRow())
120         if item:
121                 self.deleteVersion(qvariant_converter.getString(item.data(Qt.UserRole)))
122         self.emit(SIGNAL("completeChanged()"))
123     
124     def rowChanged(self):
125         """
126         Slot called when the user select an entry from the version list.
127         """
128         if self.isDefaultVersion(self.currentVersion()):
129             self.disableRemoveButton()
130         else:
131             self.enableRemoveButton()
132         self.emit(SIGNAL("completeChanged()"))
133
134     def updateClicked(self):
135         """
136         Slot called when the user clicks on the 'update' button. It checks for
137         update (TO BE IMPLEMENTED).
138         """
139         pass    
140
141     ####
142     
143     def storeVersion(self, directory):
144         """
145         Stores the directory selected by the user in the QSettings.
146         """
147         versions = self.versions()
148         versions = set(versions + [directory])
149         self.setVersions(list(versions))
150     
151     def deleteVersion(self, directory):
152         """
153         Removes the given directory from the QSettings.
154         """
155         versions = [os.path.normpath(path) for path in self.versions()]
156         versions.remove(os.path.normpath(directory))
157         self.setVersions(versions)
158     
159     def resetVersionList(self):
160         """
161         Remove all the version entries from the list.
162         """
163         self.pageContent.versionList.clear()
164     
165     def insertListElement(self, directory):
166         """
167         Inserts the given directory in the version list and returns the
168         inserted item.
169         """
170         if bertos_utils.isBertosDir(directory):
171             item = QListWidgetItem(QIcon(":/images/ok.png"), bertos_utils.bertosVersion(directory) + " (\"" + os.path.normpath(directory) + "\")")
172             item.setData(Qt.UserRole, qvariant_converter.convertString(directory))
173             self.pageContent.versionList.addItem(item)
174             return item
175         elif len(directory) > 0:
176             item = QListWidgetItem(QIcon(":/images/warning.png"), "UNKNOWN" + " (\"" + os.path.normpath(directory) + "\")")
177             item.setData(Qt.UserRole, qvariant_converter.convertString(directory))
178             self.pageContent.versionList.addItem(item)
179             return item
180     
181     def fillVersionList(self):
182         """
183         Fills the version list with all the BeRTOS versions founded in the QSettings.
184         """
185         versions = set([])
186         if os.name == "nt":
187             import winreg_importer
188             versions |= set([os.path.normpath(dir) for dir in winreg_importer.getBertosDirs()])
189         versions |= set([os.path.normpath(dir) for dir in self.versions()])
190         selected = self.projectInfo("BERTOS_PATH")
191         for directory in versions:
192             item = self.insertListElement(directory)
193             if selected and selected == directory:
194                 self.setCurrentItem(item)
195         if not selected:
196             latest_version_item = self.latestVersionItem()
197             if latest_version_item:
198                 self.setCurrentItem(latest_version_item)
199     
200     def disableRemoveButton(self):
201         """
202         Disable the Remove button.
203         """
204         self.pageContent.removeButton.setEnabled(False)
205
206     def enableRemoveButton(self):
207         """
208         Enable the Remove button.
209         """
210         self.pageContent.removeButton.setEnabled(True)
211     
212     def latestVersionItem(self):
213         """
214         Returns the latest BeRTOS version founded.
215         """
216         latest_version_item = None
217         for index in range(self.pageContent.versionList.count()):
218             item = self.pageContent.versionList.item(index)
219             if not latest_version_item:
220                 latest_version_item = item
221             version = item.text().split(" (")[0]
222             latest = latest_version_item.text().split(" (")[0]
223             if version != "UNKNOWN" and version > latest:
224                 latest_version_item = item
225         return latest_version_item
226     
227     def setCurrentItem(self, item):
228         """
229         Select the given item in the version list.
230         """
231         self.pageContent.versionList.setCurrentItem(item)
232     
233     def currentItem(self):
234         """
235         Returns the current selected item.
236         """
237         return self.pageContent.versionList.currentItem()
238     
239     def currentVersion(self):
240         """
241         Return the path of the selected version.
242         """
243         current = self.currentItem()
244         if current:
245             return qvariant_converter.getString(current.data(Qt.UserRole))
246         else:
247             return None
248     
249     def isDefaultVersion(self, version):
250         """
251         Returns True if the given version is one of the default versions.
252         """
253         if os.name == "nt":
254             import winreg_importer
255             if version in winreg_importer.getBertosDirs():
256                 return True
257         return False
258