Use the local BeRTOS version instead of the original one
[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 # $Id$
32 #
33 # Author: Lorenzo Berni <duplo@develer.com>
34 #
35
36 import os
37
38 from PyQt4.QtGui import *
39 from BWizardPage import *
40 import bertos_utils
41 import qvariant_converter
42
43 from const import *
44
45 class BVersionPage(BWizardPage):
46     """
47     Page of the wizard that permits to choose which BeRTOS version the user wants
48     to use. This page show some pieces of information about the version.
49     """
50     
51     def __init__(self, edit=False):
52         self._edit = edit
53         BWizardPage.__init__(self, UI_LOCATION + "/bertos_versions.ui")
54         self.setTitle(self.tr("Select the BeRTOS directory"))
55         self.setSubTitle(self.tr("The project created will be based on the BeRTOS version found"))
56
57     ## Overloaded QWizardPage methods ##
58     
59     def isComplete(self):
60         """
61         Overload of the QWizardPage isComplete method.
62         """
63         if self.pageContent.versionList.currentRow() != -1:
64             sources_path = qvariant_converter.getString(self.pageContent.versionList.currentItem().data(Qt.UserRole))
65             # Remove the trailing slash
66             if sources_path.endswith(os.sep):
67                 sources_path = sources_path[:-1]
68             self.setProjectInfo("SOURCES_PATH", sources_path)
69             return True
70         else:
71             return False
72     
73     ####
74     
75     ## Overloaded BWizardPage methods ##
76
77     def connectSignals(self):
78         """
79         Overload of the BWizardPage connectSignals method.
80         """
81         self.connect(self.pageContent.versionList, SIGNAL("itemSelectionChanged()"), self.rowChanged)
82         self.connect(self.pageContent.addButton, SIGNAL("clicked()"), self.addVersion)
83         self.connect(self.pageContent.removeButton, SIGNAL("clicked()"), self.removeVersion)
84         # Fake signal connection for the update button
85         self.connect(self.pageContent.updateButton, SIGNAL("clicked()"), self.updateClicked)
86     
87     def reloadData(self):
88         """
89         Overload of the BWizardPage reloadData method.
90         """
91         self.resetVersionList()
92         self.pageContent.versionList.setCurrentRow(-1)
93         self.fillVersionList()
94     
95     def setupUi(self):
96         """
97         Overload of the BWizardPage setupUi method.
98         """
99         self.pageContent.updateButton.setVisible(False)
100     
101     ####
102     
103     ## Slots ##
104     
105     def addVersion(self):
106         """
107         Slot called when the user add a BeRTOS version.
108         """
109         directory = QFileDialog.getExistingDirectory(self, self.tr("Choose a directory"), "", QFileDialog.ShowDirsOnly | QFileDialog.DontResolveSymlinks)
110         if not directory.isEmpty():
111             self.storeVersion(unicode(directory))
112             self.pageContent.versionList.clear()
113             self.fillVersionList()
114             self.emit(SIGNAL("completeChanged()"))
115     
116     def removeVersion(self):
117         """
118         Slot called when the user remove a BeRTOS version.
119         """
120         item = self.pageContent.versionList.takeItem(self.pageContent.versionList.currentRow())
121         if item:
122                 self.deleteVersion(qvariant_converter.getString(item.data(Qt.UserRole)))
123         self.emit(SIGNAL("completeChanged()"))
124     
125     def rowChanged(self):
126         """
127         Slot called when the user select an entry from the version list.
128         """
129         if self.isDefaultVersion(self.currentVersion()):
130             self.disableRemoveButton()
131         else:
132             self.enableRemoveButton()
133         self.emit(SIGNAL("completeChanged()"))
134
135     def updateClicked(self):
136         """
137         Slot called when the user clicks on the 'update' button. It checks for
138         update (TO BE IMPLEMENTED).
139         """
140         pass    
141
142     ####
143     
144     def storeVersion(self, directory):
145         """
146         Stores the directory selected by the user in the QSettings.
147         """
148         versions = self.versions()
149         versions = set(versions + [directory])
150         self.setVersions(list(versions))
151     
152     def deleteVersion(self, directory):
153         """
154         Removes the given directory from the QSettings.
155         """
156         versions = [os.path.normpath(path) for path in self.versions()]
157         versions.remove(os.path.normpath(directory))
158         self.setVersions(versions)
159     
160     def resetVersionList(self):
161         """
162         Remove all the version entries from the list.
163         """
164         self.pageContent.versionList.clear()
165     
166     def insertListElement(self, directory):
167         """
168         Inserts the given directory in the version list and returns the
169         inserted item.
170         """
171         if bertos_utils.isBertosDir(directory):
172             item = QListWidgetItem(QIcon(":/images/ok.png"), bertos_utils.bertosVersion(directory) + " (\"" + os.path.normpath(directory) + "\")")
173             item.setData(Qt.UserRole, qvariant_converter.convertString(directory))
174             self.pageContent.versionList.addItem(item)
175             return item
176         elif len(directory) > 0:
177             item = QListWidgetItem(QIcon(":/images/warning.png"), "UNKNOWN" + " (\"" + os.path.normpath(directory) + "\")")
178             item.setData(Qt.UserRole, qvariant_converter.convertString(directory))
179             self.pageContent.versionList.addItem(item)
180             return item
181     
182     def fillVersionList(self):
183         """
184         Fills the version list with all the BeRTOS versions founded in the QSettings.
185         """
186         versions = set([])
187         if self._edit:
188             versions.add(self.projectInfo("SOURCES_PATH"))
189         if os.name == "nt":
190             import winreg_importer
191             versions |= set([os.path.normpath(dir) for dir in winreg_importer.getBertosDirs()])
192         versions |= set([os.path.normpath(dir) for dir in self.versions()])
193         selected = self.projectInfo("SOURCES_PATH")
194         for directory in versions:
195             item = self.insertListElement(directory)
196             if selected and selected == directory:
197                 self.setCurrentItem(item)
198         if not selected:
199             latest_version_item = self.latestVersionItem()
200             if latest_version_item:
201                 self.setCurrentItem(latest_version_item)
202     
203     def disableRemoveButton(self):
204         """
205         Disable the Remove button.
206         """
207         self.pageContent.removeButton.setEnabled(False)
208
209     def enableRemoveButton(self):
210         """
211         Enable the Remove button.
212         """
213         self.pageContent.removeButton.setEnabled(True)
214     
215     def latestVersionItem(self):
216         """
217         Returns the latest BeRTOS version founded.
218         """
219         latest_version_item = None
220         for index in range(self.pageContent.versionList.count()):
221             item = self.pageContent.versionList.item(index)
222             if not latest_version_item:
223                 latest_version_item = item
224             version = item.text().split(" (")[0]
225             latest = latest_version_item.text().split(" (")[0]
226             if version != "UNKNOWN" and version > latest:
227                 latest_version_item = item
228         return latest_version_item
229     
230     def setCurrentItem(self, item):
231         """
232         Select the given item in the version list.
233         """
234         self.pageContent.versionList.setCurrentItem(item)
235     
236     def currentItem(self):
237         """
238         Returns the current selected item.
239         """
240         return self.pageContent.versionList.currentItem()
241     
242     def currentVersion(self):
243         """
244         Return the path of the selected version.
245         """
246         current = self.currentItem()
247         if current:
248             return qvariant_converter.getString(current.data(Qt.UserRole))
249         else:
250             return None
251     
252     def isDefaultVersion(self, version):
253         """
254         Returns True if the given version is one of the default versions.
255         """
256         if os.name == "nt":
257             import winreg_importer
258             if version in winreg_importer.getBertosDirs():
259                 return True
260         return False
261