1 # Copyright (c) Stas Shtin, 2010
3 # This file is part of IPyPBX.
5 # IPyPBX is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # IPyPBX is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with IPyPBX. If not, see <http://www.gnu.org/licenses/>.
20 from ipypbx import controllers, ui
21 from PyQt4 import QtCore, QtGui, QtSql
24 # Working directory path.
25 PREFIX = os.path.expanduser('~/.ipypbx')
31 def setupDb(prefix=PREFIX, dbname=DB_NAME):
36 # Create it if necessary.
38 if not os.path.exists(prefix):
40 os.mkdir(prefix, 0700)
44 # Connect to SQLite database.
45 db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
46 db.setDatabaseName(dbname)
50 # Load script from local file.
51 sql_script = open(os.path.join(os.path.dirname(__file__), 'create.sql')).read()
53 # Split into individual queries.
54 sql_queries = sql_script.split(';')
56 # Execute all queries except last that is empty.
57 query = QtSql.QSqlQuery()
58 for query_string in sql_queries[:-1]:
59 query.exec_(query_string)
63 # Something went horribly wrong.
64 QtGui.QMessageBox.warning(
65 None, "Fatal Error", "Database Error: %s" % db.lastError().text())
69 if __name__ == '__main__':
70 app = QtGui.QApplication(sys.argv)
73 locale = QtCore.QLocale.system().name()
74 translator = QtCore.QTranslator()
76 if translator.load("ipypbx_%s" % locale.toLower(), "ipypbx/locale"):
77 QtGui.QApplication.installTranslator(translator)
79 # Initialize main window.
80 db, created = setupDb()
81 main = QtGui.QMainWindow()
82 views = ui.Ui_MainWindow()
88 for basename in ('Connection', 'SipProfile', 'Domain', 'Gateway',
89 'Endpoint', 'Extension'):
90 main.controllers[basename.lower()] = getattr(
91 controllers, basename + 'Controller'
92 )(parent=main, views=views)
95 connection_model = main.controllers['connection'].model
96 connection_index = connection_model.index(0, 0)
98 # Get first row index.
99 first_row_id, ok = connection_model.data(
100 connection_index.sibling(0, 0)).toInt()
103 # We have to explicitly emit row selection signal since previous state
106 main.controllers['connection'].view_list.selectionModel(),
107 QtCore.SIGNAL('currentRowChanged(QModelIndex, QModelIndex)'),
108 connection_index, connection_index)
110 # TODO: not needed anymore?
112 main.controllers['connection'].add()
113 sys.exit(app.exec_())