Add/edit all tables. Data gets save to DB at last\!
[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     else:
61         # Something went horribly wrong.
62         QtGui.QMessageBox.warning(
63             None, "Fatal Error", "Database Error: %s" % db.lastError().text())
64         sys.exit(1)
65
66
67 if __name__ == '__main__':
68     app = QtGui.QApplication(sys.argv)
69
70     locale = QtCore.QLocale.system().name()
71     translator = QtCore.QTranslator()
72     
73     if translator.load("ipypbx_%s" % locale.toLower(), "ipypbx/locale"):
74         QtGui.QApplication.installTranslator(translator)
75
76     setupDb()
77     main = QtGui.QMainWindow()
78     views = ui.Ui_MainWindow()
79     views.setupUi(main)
80     main.show()
81
82     main.controllers = []
83     for basename in ('Connection', 'SipProfile', 'Domain', 'Gateway',
84                      'Endpoint', 'Extension'):
85         main.controllers.append(getattr(
86             controllers, basename + 'Controller'
87             )(parent=main, views=views))
88
89     connection_model = main.controllers[0].model
90     connection_index = connection_model.index(0, 0)
91     first_row_id, ok = connection_model.data(
92         connection_index.sibling(0, 0)).toInt()
93
94     if ok:        
95         QtCore.QObject.emit(
96             main.controllers[0].view_list.selectionModel(),
97             QtCore.SIGNAL('currentRowChanged(QModelIndex, QModelIndex)'),
98             connection_index, connection_index)
99     
100
101
102     sys.exit(app.exec_())
103