Add missing files
[klaudia.git] / src / wizardw.py
blobea78d76bdfe82c22e8a095812e36e30b3007540a
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
5 # Imports
6 import dbus, jack, os
7 import shared, ui_wizard
8 from PyQt4.QtCore import QFile, QIODevice, QTextStream, SIGNAL
9 from PyQt4.QtGui import QWizard
10 from PyKDE4.kdecore import i18n
11 from PyKDE4.kdeui import KIconLoader, KMessageBox
13 # Connect to Jack DBus Service
14 bus = dbus.SessionBus()
15 jackBus = bus.get_object("org.jackaudio.service", "/org/jackaudio/Controller")
17 # Check if Jack is started, start it if not
18 if not bool(jackBus.IsStarted()):
19 print "Jack is not started, trying to starting it now..."
20 jackBus.StartServer()
22 # Enable Jack for Klaudia
23 try:
24 jack.attach("Klaudia")
25 jackStarted = True
26 except:
27 jackStarted = False
30 # Main Window
31 class WizardW(QWizard, ui_wizard.Ui_WizardW):
32 def __init__(self, *args):
33 QWizard.__init__(self, *args)
34 self.setupUi(self)
36 # Check for Jack before anything else
37 if not jackStarted:
38 print "Could not start Jack, quiting..."
39 KMessageBox.error(self, i18n("Could not auto-start Jack.\nPlease close all audio applications and start Jack.\nKlaudia will quit now."), i18n("Error"))
40 exit(-1)
42 # Set-up
43 self.b_icon.setIcon("inode-directory")
44 self.b_icon.setIconType(KIconLoader.Desktop, KIconLoader.Place)
46 # Connections
47 self.connect(self.b_icon, SIGNAL("iconChanged(QString)"), self.changedIcon)
48 self.connect(self.r_newp, SIGNAL("clicked(bool)"), self.enableNext)
49 self.connect(self.r_loadp, SIGNAL("clicked()"), self.checkUrl_Load)
50 self.connect(self.url_newp, SIGNAL("urlSelected(KUrl)"), self.checkForNewProjectFolder)
51 self.connect(self.url_loadp, SIGNAL("urlSelected(KUrl)"), self.checkForProjectFile)
52 self.connect(self.url_newp, SIGNAL("textChanged(QString)"), self.checkUrl_New)
53 self.connect(self.url_loadp, SIGNAL("textChanged(QString)"), self.checkUrl_Load)
54 self.connect(self, SIGNAL("currentIdChanged(int)"), self.changedPage)
55 self.connect(self, SIGNAL("accepted()"), self.Accept)
57 def changedIcon(self, icon):
58 shared.p_icon = icon
60 def changedPage(self, pageN):
61 if (pageN == 1):
62 if (self.r_loadp.isChecked()):
63 self.accept()
64 else:
65 self.checkUrl_New()
67 def checkForNewProjectFolder(self, pUrl):
68 if not pUrl.isLocalFile():
69 KMessageBox.error(self, i18n("The Selected folder cannot be used for a Klaudia project!"), i18n("Error"))
71 def checkForProjectFile(self, pUrl):
72 if not pUrl.isLocalFile():
73 KMessageBox.error(self, i18n("The Selected folder cannot be used for a Klaudia project!"), i18n("Error"))
74 else:
75 if (not os.path.exists(pUrl.toLocalFile()+"/project.klaudia")):
76 KMessageBox.sorry(self, i18n("The Selected folder does not contain a Klaudia project!"), i18n("Sorry"))
78 def checkUrl_New(self, url=""):
79 if not url: url = self.url_newp.text()
80 if (os.path.exists(url)):
81 self.enableFinish(True)
82 else:
83 self.enableFinish(False)
85 def checkUrl_Load(self, url=""):
86 if not url: url = self.url_loadp.text()
87 if (os.path.exists(url+"/project.klaudia")):
88 self.enableNext(True)
89 else:
90 self.enableNext(False)
92 def enableNext(self, check):
93 self.button(QWizard.NextButton).setEnabled(check)
95 def enableFinish(self, check):
96 self.button(QWizard.FinishButton).setEnabled(check)
98 def Accept(self):
99 if (self.r_loadp.isChecked()):
100 url = self.url_loadp.text()
101 shared.p_folder = url
102 shared.setIconFromFolder(url)
103 else:
104 url = self.url_newp.text()
105 shared.p_folder = url
106 filename = QFile(url+"/project.klaudia")
107 if not filename.open(QIODevice.WriteOnly):
108 KMessageBox.error(self, i18n("Cannot write to selected directory - Permission Denied"), i18n("Error"))
109 shared.exit_(-2)
110 #raise IOError, unicode(filename.errorString())
111 stream = QTextStream(filename)
112 stream.setCodec("UTF-8")
113 stream << ("<?xml version='1.0' encoding='UTF-8'?>\n"
114 "<!DOCTYPE KLAUDIA>\n"
115 "<KLAUDIA VERSION='0.2'>\n"
116 "<Properties>\n"
117 " <Name>%s</Name>\n"
118 " <Author>%s</Author>\n"
119 " <Composer>%s</Composer>\n"
120 " <Album>%s</Album>\n"
121 " <Track>%s</Track>\n"
122 " <BPM>%s</BPM>\n"
123 " <Time-Start>00:00:00</Time-Start>\n"
124 " <Time-End>%s</Time-End>\n"
125 "</Properties>\n"
126 "</KLAUDIA>\n" % (self.line_name.text(), self.line_author.text(), self.line_composer.text(),
127 self.line_album.text(), self.spin_track.text(), self.spin_bpm.text(), self.time_total.text()))
128 iconfile = open(url+"/.directory", "w")
129 iconfile.write("[Desktop Entry]\nIcon=%s" % str(self.b_icon.icon()))
130 iconfile.close()