Don't permit to use non ascii character in destination directory field.
[bertos.git] / wizard / BFolderPage.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
42 from const import *
43
44 class BFolderPage(BWizardPage):
45     """
46     Initial page of the wizard. Permit to select the project name and the directory
47     where the project will be created.
48     """
49     
50     def __init__(self):
51         BWizardPage.__init__(self, UI_LOCATION + "/dir_select.ui")
52         self.setTitle(self.tr("Select the project name"))
53         self.initializeAttributes()
54     
55     ## Overloaded QWizardPage methods ##
56
57     def isComplete(self):
58         """
59         Overload of the QWizardPage isComplete method.
60         """
61         self.setDefaultFolder(self._destination_folder)
62         if self.pageContent.projectPath.text() != "None":
63             self.setProjectInfo("PROJECT_PATH", unicode(self.pageContent.projectPath.text()))
64             return True
65         else:
66             return False
67     
68     ####
69
70     ## Overloaded BWizardPage methods ##
71
72     def setupUi(self):
73         """
74         Overload of the BWizardPage setupUi method.
75         """
76         self.pageContent.warningLabel.setVisible(False)
77     
78     def connectSignals(self):
79         """
80         Overload of the BWizardPage connectSignals method.
81         """
82         self.connect(self.pageContent.nameEdit, SIGNAL("textChanged(const QString)"), self.nameChanged)
83         self.connect(self.pageContent.directoryEdit, SIGNAL("textChanged(const QString)"), self.directoryChanged)
84         self.connect(self.pageContent.directoryButton, SIGNAL("clicked()"), self.selectDirectory)
85     
86     ####
87
88     ## Slots ##
89     
90     def nameChanged(self, name):
91         """
92         Slot called when the project name is changed manually by the user.
93         """
94         try:
95             name = unicode(name).encode("ascii")
96         except UnicodeEncodeError:
97             name = self._project_name
98             self.pageContent.nameEdit.setText(name)
99         self._project_name = unicode(name).replace(" ", "_")
100         self.setProjectPath()
101     
102     def directoryChanged(self, directory):
103         """
104         Slot called when the project folder is changed manually by the user.
105         """
106         try:
107             directory = unicode(directory).encode("ascii")
108         except UnicodeEncodeError:
109             directory = self._destination_folder
110             self.pageContent.directoryEdit.setText(directory)
111         self._destination_folder = directory
112         self.setProjectPath()
113
114     def selectDirectory(self):
115         """
116         Slot called when the project folder is changed using the file dialog.
117         """
118         directory = unicode(QFileDialog.getExistingDirectory(self, self.tr("Open Directory"), self.pageContent.directoryEdit.text(), QFileDialog.ShowDirsOnly))
119         if len(directory) > 0:
120             self.pageContent.directoryEdit.setText(QDir.toNativeSeparators(directory))
121
122     ####
123     
124     def initializeAttributes(self):
125         """
126         Initializes the page attributes to the default values.
127         """
128         self._project_name = ""
129         stored_folder = self.defaultFolder()
130         if stored_folder != "":
131             self._destination_folder = stored_folder
132         elif os.name == "nt":
133             from win32com.shell import shell, shellcon
134             self._destination_folder = shell.SHGetFolderPath(0, shellcon.CSIDL_PERSONAL, 0, 0)
135             del shell
136             del shellcon
137         else:
138             self._destination_folder = os.path.expanduser("~")
139         self.pageContent.directoryEdit.setText(self._destination_folder)
140     
141     def setProjectPath(self):
142         """
143         Analyzes the page attributes and generates the path string.
144         """
145         if self._destination_folder != "" and self._project_name <> "":
146             if not self._destination_folder.endswith(os.sep):
147                 self._destination_folder += os.sep
148             self.pageContent.projectPath.setText(QDir.toNativeSeparators(self._destination_folder + self._project_name))
149             if os.path.exists(self._destination_folder + self._project_name):
150                 self.pageContent.warningLabel.setVisible(True)
151                 self.pageContent.warningLabel.setText(self.tr("<font color='#FF0000'>Warning: the selected directory exists, \
152                     it will be destroyed with all contained subdirectories and files...</font>"))
153             else:
154                 self.pageContent.warningLabel.setVisible(False)
155                 self.pageContent.warningLabel.setText("")
156         else:
157             self.pageContent.projectPath.setText("None")
158             self.pageContent.warningLabel.setVisible(False)
159             self.pageContent.warningLabel.setText("")
160         self.emit(SIGNAL("completeChanged()"))