Database updates; Don't require jackdbus start
[klaudia.git] / src / w_klaudia.py
blob41f7dc62e7a553bd0a66f56a5005a44388bbd432
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 # Imports (Global)
5 import dbus, os, sys
6 from PyQt4.QtCore import Qt, QSettings, QString, QVariant, SIGNAL
7 from PyQt4.QtGui import QFileDialog, QIcon, QMainWindow, QMessageBox
9 # Imports (Custom)
10 import ui_klaudia
11 from klaudia_launcher import KlaudiaLauncher
13 # Properly convert QString to str
14 def QStringStr(string):
15 return str(unicode(string).encode('utf-8'))
17 # QLineEdit and QPushButtom combo
18 def getAndSetPath(parent, current_path, lineEdit):
19 new_path = QFileDialog.getExistingDirectory(parent, parent.tr("Set Path"), current_path, QFileDialog.ShowDirsOnly)
20 if not new_path.isEmpty():
21 lineEdit.setText(new_path)
22 return new_path
24 # DBus connections
25 class DBus(object):
26 __slots__ = [
27 'loopBus',
28 'jackBus',
29 'controlBus',
30 'studioBus',
31 'appBus',
33 DBus = DBus()
35 # Main Window
36 class KlaudiaMainW(QMainWindow, ui_klaudia.Ui_KlaudiaMainW):
37 def __init__(self, parent=None):
38 super(KlaudiaMainW, self).__init__(parent)
39 self.setupUi(self)
41 # Check for Jack
42 if DBus.jackBus and not bool(DBus.jackBus.IsStarted()):
43 try:
44 DBus.jackBus.StartServer()
45 except:
46 pass
48 # Load Settings (GUI state)
49 self.settings = QSettings()
50 self.loadSettings()
52 self.launcher = KlaudiaLauncher(self, self.settings)
54 # Set-up GUI
55 self.b_start.setIcon(self.getIcon("go-next"))
56 self.b_add.setIcon(self.getIcon("list-add"))
57 self.b_refresh.setIcon(self.getIcon("view-refresh"))
58 self.b_open.setIcon(self.getIcon("document-open"))
59 self.b_start.setEnabled(False)
60 self.b_add.setEnabled(False)
61 self.setWindowIcon(self.getIcon("klaudia"))
63 self.test_url = True
64 self.test_selected = False
65 self.studio_root_folder = QString(os.getenv("HOME"))
67 self.le_url.setText(self.studio_root_folder)
68 self.co_sample_rate.addItem(str(DBus.jackBus.GetSampleRate()) if DBus.jackBus else "44100")
69 self.b_refresh.setText("")
71 self.refreshStudioList()
73 if (DBus.controlBus):
74 self.enableLADISH(True)
75 else:
76 for path in os.getenv("PATH").split(":"):
77 if (os.path.exists(os.path.join(path, "ladishd"))):
78 break
79 else:
80 self.enableLADISH(False)
82 self.connect(self.b_start, SIGNAL("clicked()"), self.launcher.startApp)
83 self.connect(self.b_add, SIGNAL("clicked()"), self.launcher.addAppToLADISH)
84 self.connect(self.b_refresh, SIGNAL("clicked()"), self.refreshStudioList)
86 self.connect(self.co_ladi_room, SIGNAL("currentIndexChanged(int)"), self.checkSelectedRoom)
87 self.connect(self.groupLADISH, SIGNAL("toggled(bool)"), self.enableLADISH)
89 self.connect(self.le_url, SIGNAL("textChanged(QString)"), self.checkFolderUrl)
91 self.connect(self.b_open, SIGNAL("clicked()"), lambda: getAndSetPath(self, self.le_url.text(), self.le_url))
93 def refreshStudioList(self):
94 self.co_ladi_room.clear()
95 self.co_ladi_room.addItem("<Studio Root>")
96 if (DBus.controlBus):
97 studio_bus = DBus.loopBus.get_object("org.ladish", "/org/ladish/Studio")
98 studio_list_dump = studio_bus.GetRoomList()
99 for i in range(len(studio_list_dump)):
100 self.co_ladi_room.addItem(str(studio_list_dump[i][0]).replace("/org/ladish/Room","")+" - "+studio_list_dump[i][1]['name'])
102 def checkSelectedRoom(self, co_n):
103 if (co_n == -1 or not DBus.controlBus):
104 pass
105 elif (co_n == 0):
106 DBus.studioBus = DBus.loopBus.get_object("org.ladish", "/org/ladish/Studio")
107 DBus.appBus = dbus.Interface(DBus.studioBus, 'org.ladish.AppSupervisor')
108 self.b_open.setEnabled(True)
109 self.le_url.setEnabled(True)
110 self.le_url.setText(self.studio_root_folder)
111 else:
112 room_number = QStringStr(self.co_ladi_room.currentText()).split(" ")[0]
113 room_name = "/org/ladish/Room"+room_number
114 DBus.studioBus = DBus.loopBus.get_object("org.ladish", room_name)
115 DBus.appBus = dbus.Interface(DBus.studioBus, 'org.ladish.AppSupervisor')
116 room_properties = DBus.studioBus.GetProjectProperties()
117 if (len(room_properties[1]) > 0):
118 self.b_open.setEnabled(False)
119 self.le_url.setEnabled(False)
120 self.le_url.setText(QString(room_properties[1]['dir']))
121 else:
122 self.b_open.setEnabled(True)
123 self.le_url.setEnabled(True)
124 self.studio_root_folder = self.le_url.text()
126 def enableLADISH(self, yesno):
127 self.groupLADISH.setCheckable(False)
129 if (yesno):
130 try:
131 DBus.controlBus = DBus.loopBus.get_object("org.ladish", "/org/ladish/Control")
132 self.groupLADISH.setTitle(self.tr("LADISH is enabled"))
133 except:
134 self.groupLADISH.setEnabled(False)
135 self.groupLADISH.setTitle(self.tr("LADISH is sick"))
136 return
138 DBus.studioBus = DBus.loopBus.get_object("org.ladish", "/org/ladish/Studio")
139 DBus.appBus = dbus.Interface(DBus.studioBus, 'org.ladish.AppSupervisor')
141 self.refreshStudioList()
142 self.checkGUI()
144 else:
145 self.groupLADISH.setEnabled(False)
146 self.groupLADISH.setTitle(self.tr("LADISH is not available"))
148 def checkGUI(self, test_selected=None):
149 if (test_selected != None):
150 self.test_selected = test_selected
152 if (self.test_url and self.test_selected):
153 self.b_add.setEnabled(bool(DBus.controlBus))
154 self.b_start.setEnabled(True)
155 else:
156 self.b_add.setEnabled(False)
157 self.b_start.setEnabled(False)
159 def checkFolderUrl(self, qurl):
160 url = QStringStr(qurl)
161 if (os.path.exists(url)):
162 self.test_url = True
163 if (self.le_url.isEnabled()):
164 self.studio_root_folder = url
165 else:
166 self.test_url = False
167 self.checkGUI()
169 def isRoom(self):
170 return not self.le_url.isEnabled()
172 def isOldLadish(self):
173 try:
174 DBus.loopBus.get_object("org.ladish", "/org/ladish/LashServer")
175 is_old = False
176 except:
177 is_old = True
178 return is_old
180 def getAppBus(self):
181 return DBus.appBus
183 def getSampleRate(self):
184 srate_try = self.co_sample_rate.currentText().toInt()
185 return srate_try[0] if srate_try[1] else 44100
187 def getBPM(self):
188 bpm_try = self.sb_bpm.text().toInt()
189 return bpm_try[0] if bpm_try[1] else 130.0
191 def getProjectFolder(self):
192 return self.le_url.text()
194 def getIcon(self, icon):
195 return QIcon.fromTheme(icon, QIcon(""))
197 def saveSettings(self):
198 self.settings.setValue("Geometry", QVariant(self.saveGeometry()))
199 self.launcher.saveSettings()
201 def loadSettings(self):
202 self.restoreGeometry(self.settings.value("Geometry").toByteArray())
204 def closeEvent(self, event):
205 self.saveSettings()
206 return QMainWindow.closeEvent(self, event)