summaryrefslogtreecommitdiff
path: root/winMain.py
blob: b34d06982a0723c593bb072ef69de471ae2758c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
from PyQt4 import QtGui, QtCore, QtSvg
from traceback import print_exc
import time
import sys
import copy

from misc import *
from clSettings import settings,mpdSettings
from clMonty import monty

#from plugins import *
import plugins

from winConnect import winConnect
from winSettings import winSettings

from wgSongList import SongList
from wgPlaylist import Playlist



class winMain(QtGui.QMainWindow):
	"""The winMain class is mpc's main window, showing the playlists and control-interface"""
	" list of plugins that are loaded. name=>Plugin"
	plugins={}

	" menus"
	mConnect=None
	mDisconnect=None
	
	" connection window"
	wConnect=None
	wSettings=None

	"Other vars"
	" all objects which should be disabled when there is no connection"
	plugins=None

	def __init__(self, parent=None):
		QtGui.QWidget.__init__(self, parent)
		self.setWindowTitle("montypc - An MPD client")

		self.plugins={}
		opts=QtGui.QDockWidget.DockWidgetClosable|QtGui.QDockWidget.DockWidgetMovable
		for p in plugins.listPlugins():
			pkg,name=p[0],p[1]
			# WARNING - dirty hack ahead!
			plugin=eval('%s.%s'%(pkg,name))(self)
			self.plugins[plugin.getName().lower()]=plugin
			dock=plugin.getDockWidget(opts)
			if dock:
				self.addDockWidget(QtCore.Qt.TopDockWidgetArea, plugin.getDockWidget(opts))
		
		self.wConnect=winConnect()

		self.statusBar()	# create a statusbar
		mBar=self.menuBar()	# create a menubar
		# menu file
		m=mBar.addMenu("File")
		m.setTearOffEnabled(True)
		# connect
		self.mConnect=m.addAction('Connect ...', self.wConnect.monitor)
		self.mConnect.setIcon(QtGui.QIcon('gfx/connect.png'))
		# disconnect
		self.mDisconnect=m.addAction('Disconnect', monty.disconnect)
		self.mDisconnect.setIcon(QtGui.QIcon('gfx/disconnect.png'))
		# separator
		m.addSeparator()
		# quit
		m.addAction("Quit", QtCore.QCoreApplication.exit).setIcon(QtGui.QIcon('gfx/gtk-quit.svg'))

		# menu options
		m=mBar.addMenu("Options")
		m.setTearOffEnabled(True)
		# settings
		m.addAction("Settings", self.showWinSettings).setIcon(QtGui.QIcon('gfx/gtk-preferences.svg'))

		# menu layout
		m=mBar.addMenu("Layout")
		m.setTearOffEnabled(True)
		m.addAction('Save layout', self.saveLayout)
		m.addAction('Restore layout', self.restoreLayout)
		m.addSeparator()
		# can not use iterators, as that gives some creepy error 'bout c++
		actions=self.createPopupMenu().actions()
		for i in xrange(len(actions)):
			m.addAction(actions[i])

		self.setDockOptions(QtGui.QMainWindow.AllowNestedDocks \
				|QtGui.QMainWindow.AllowTabbedDocks \
				|QtGui.QMainWindow.VerticalTabs)
		self.setDockNestingEnabled(True)

		size=settings.get('winMain.size', '800 600')
		try:
			w=int(size[0:size.find(' ')].strip())
			h=int(size[size.find(' '):].strip())
		except:
			w,h=800,600
		self.resize(w,h)
		self.restoreLayout()

		" add event handlers"
		monty.addListener('onReady', self.onReady)
		monty.addListener('onConnect', self.onConnect)
		monty.addListener('onDisconnect', self.onDisconnect)
		monty.addListener('onUpdateDBStart', self.onUpdateDBStart)
		monty.addListener('onUpdateDBFinish', self.onUpdateDBFinish)

		self.enableAll(True)
		self.setWindowIcon(appIcon)
		# set icon in system tray
		self.wConnect.monitor()

		self.show()
		#self.showWinSettings()
		doEvents()


	mMenuVisible=None
	def createPopupMenu(self):
		ret=QtGui.QMenu('Test', self)
		if self.mMenuVisible==None:
			# create checkable menu
			a=QtGui.QAction('Menubar', self)
			a.setCheckable(True)
			a.setChecked(True)
			self.connect(a, QtCore.SIGNAL('toggled(bool)'), self.switchMenubar)
			
			self.mMenuVisible=a
		ret.addAction(self.mMenuVisible)
		ret.addSeparator()
		actions=QtGui.QMainWindow.createPopupMenu(self).actions()
		for i in xrange(len(actions)-1):
			ret.addAction(actions[i])
		return ret
	def switchMenubar(self, val):
		self.menuBar().setVisible(val)
	def setStatus(self, status):
		"""Set the text of the statusbar."""
		self.statusBar().showMessage(status)

	def saveLayout(self):
		f=open('layout', 'wb')
		f.write(self.saveState())
		f.close()
	def restoreLayout(self):
		try:
			f=open('layout', 'rb')
			self.restoreState(f.read())
			f.close()
		except:
			pass

	def showWinSettings(self):
		if not self.wSettings:
			self.wSettings=winSettings(self)
		self.wSettings.show()

	def onReady(self, params):
		self.initialiseData()

	def onConnect(self, params):
		self.setStatus('Restoring library and playlist ...')
		self.mDisconnect.setEnabled(True)
		self.mConnect.setEnabled(False)
		doEvents()

	def enableAll(self, value):
		for o in self.plugins:
			try:
				self.plugins[o].o.setEnabled(value)
			except:
				pass

	def initialiseData(self):
		"""Initialise the library, playlist and some other small things"""
		self.setStatus("Filling library ...")
		doEvents()
		self.fillLibrary()
		doEvents()
		
		self.setStatus("Filling playlist ...")
		doEvents()
		self.fillPlaylist()
		doEvents()
		
		self.setStatus("Doing the rest ...")
		doEvents()
		self.enableAll(True)
		self.setStatus("")
		doEvents()
	
	def resizeEvent(self, event):
		# store new size
		settings.set('winMain.size', '%i %i'%(self.width(),self.height()))
	
	def onDisconnect(self, params):
		try:
			self.getPlaylistList().update([])
		except:
			pass
		try:
			self.getLibraryList().update([])
		except:
			pass
		self.mDisconnect.setEnabled(False)
		self.mConnect.setEnabled(True)
		self.enableAll(False)
		self.setStatus("You are disconnected. Choose File->Connect to reconnect!")

	def fillPlaylist(self):
		"""Fill the playlist."""
		try:
			self.getPlaylistList().update(monty.listPlaylist())
		except:
			pass

	def fillLibrary(self):
		"""Fill the library."""
		try:
			self.getLibraryList().update(monty.listLibrary())
		except:
			pass
	
	def getPlaylistList(self):
		try:
			return self.plugins['playlist'].getList()
		except:
			print "Missing playlist plugin!"
			return None

	def getLibraryList(self):
		try:
			return self.plugins['library'].getList()
		except:
			print "Missing library plugin!"
			return None

	def onUpdateDBFinish(self, params):
		self.setStatus('')
	def onUpdateDBStart(self, params):
		self.setStatus('Updating the database. Please wait ...')