9c8ebfe851cf18deff59d5ae4792cd5831d6d1d8
[ipypbx] / src / ipypbx / main.py
1 # Copyright (c) Stas Shtin, 2010
2
3 # This file is part of IPyPBX.
4
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.
9
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.
14
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/>.
17
18 import os
19 import sys
20 from ipypbx import controllers, ui
21 from PyQt4 import QtCore, QtGui, QtSql
22
23
24 # Working directory path.
25 PREFIX = os.path.expanduser('~/.ipypbx')
26
27 # Database file name.
28 DB_NAME = 'ipypbx.db'
29
30
31 def setupDb(prefix=PREFIX, dbname=DB_NAME):
32     """
33     Setup database.
34     """
35     created = False
36     # Create it if necessary.
37     if prefix:
38         if not os.path.exists(prefix):
39             created = True
40             os.mkdir(prefix, 0700)
41         
42         os.chdir(prefix)
43     
44     # Connect to SQLite database.
45     db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
46     db.setDatabaseName(dbname)
47     
48     if db.open():
49         if created:
50             # Load script from local file.
51             sql_script = open(os.path.join(os.path.dirname(__file__), 'create.sql')).read()
52
53             # Split into individual queries.
54             sql_queries = sql_script.split(';')
55
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)
60
61         return created
62     else:
63         # Something went horribly wrong.
64         QtGui.QMessageBox.warning(
65             None, "Fatal Error", "Database Error: %s" % db.lastError().text())
66         sys.exit(1)
67
68
69 if __name__ == '__main__':
70     app = QtGui.QApplication(sys.argv)
71
72     # Localize UI.
73     locale = QtCore.QLocale.system().name()
74     translator = QtCore.QTranslator()
75     
76     if translator.load("ipypbx_%s" % locale.toLower(), "ipypbx/locale"):
77         QtGui.QApplication.installTranslator(translator)
78
79     # Initialize main window.
80     created = setupDb()
81     main = QtGui.QMainWindow()
82     views = ui.Ui_MainWindow()
83     views.setupUi(main)
84     main.show()
85
86     # Setup controllers.
87     main.controllers = {}
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)
93
94     # Select first row.
95     connection_model = main.controllers['connection'].model
96     connection_index = connection_model.index(0, 0)
97
98     # Get first row index.
99     first_row_id, ok = connection_model.data(
100         connection_index.sibling(0, 0)).toInt()
101     
102     if ok:
103         # We have to explicitly emit row selection signal since previous state
104         # was unselected.
105         QtCore.QObject.emit(
106             main.controllers['connection'].view_list.selectionModel(),
107             QtCore.SIGNAL('currentRowChanged(QModelIndex, QModelIndex)'),
108             connection_index, connection_index)
109
110     if created:
111         main.controllers['connection'].add()
112     sys.exit(app.exec_())
113