nephilim_app: use templates for tags expanding
[nephilim.git] / nephilim / nephilim_app.py
blobb03245d1276a2fb1ec4996bf95a071ec70659544
2 # Copyright (C) 2009 Anton Khirnov <wyskas@gmail.com>
4 # Nephilim is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # Nephilim is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with Nephilim. If not, see <http://www.gnu.org/licenses/>.
18 from PyQt4 import QtGui, QtCore
19 from string import Template
21 from main_window import MainWindow
22 from common import ORGNAME, APPNAME, appIcon
23 from mpclient import MPClient
24 from settings_wg import SettingsWidget
25 from connect_wg import ConnectWidget
26 import plugins
27 import icons
29 class NephilimApp(QtGui.QApplication):
30 #### PUBLIC ####
31 # those don't change while the program is running
32 """main window object"""
33 main_win = None
34 """"MPD layer"""
35 mpclient = None
36 """"plugins interface"""
37 plugins = None
38 """settings object"""
39 settings = None
41 #### PRIVATE ####
42 """settings window"""
43 _settings_win = None
44 """connection window"""
45 _connect_win = None
47 #### PUBLIC ####
48 def __init__(self, argv):
49 QtGui.QApplication.__init__(self, argv)
51 self.setApplicationName(APPNAME)
52 self.setOrganizationName(ORGNAME)
53 self.setWindowIcon(QtGui.QIcon(appIcon))
55 def __str__(self):
56 return APPNAME
58 def exec_(self):
59 #init MPD layer
60 self.mpclient = MPClient(self)
62 #init settings
63 self.settings = QtCore.QSettings(self)
65 #init connection window
66 self._connect_win = ConnectWidget()
68 #init main window
69 self.main_win = MainWindow(self.mpclient)
71 #init plugins
72 show_settings = False # are there new plugins?
73 self.plugins = plugins.Plugins(self.main_win, self.mpclient)
74 for plugin in self.plugins.plugins():
75 if self.settings.value(plugin.name + '/load') == None:
76 show_settings = True
77 if int(self.settings.value(plugin.name + '/load', 0)):
78 self.plugins.load(plugin.name)
80 self.aboutToQuit.connect(self._cleanup)
83 if show_settings:
84 self.show_settings_win()
85 self.main_win.restore_layout()
86 self._connect_win.monitor()
88 QtGui.QApplication.exec_()
91 def show_settings_win(self):
92 if not self._settings_win:
93 self._settings_win = SettingsWidget(self.mpclient, self.plugins)
95 self._settings_win.show()
96 self._settings_win.raise_()
98 def show_connect_win(self):
99 self._connect_win.monitor()
101 def expand_tags(self, str):
102 replacements = {'musicdir' : self.settings.value('MPD/music_dir')}
103 ret = Template(str)
104 ret = ret.safe_substitute(replacements)
105 return ret
107 #### PRIVATE ####
108 def _cleanup(self):
109 # unload all plugins
110 for plugin in self.plugins.loaded_plugins():
111 plugin.unload()
113 self.main_win.on_quit()
114 self.settings.sync()