|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 // System includes |
|
19 #include <QPushButton> |
|
20 #include <QGridLayout> |
|
21 #include <QVBoxLayout> |
|
22 #include <HbMainWindow> |
|
23 #include <QTimer> |
|
24 #include <QMessageBox> |
|
25 #include <QComboBox> |
|
26 #include <QLocalSocket> |
|
27 #include <QDir> |
|
28 #include <QStringListModel> |
|
29 #include <QSettings> |
|
30 #include <HbInstance> |
|
31 #include <QLabel> |
|
32 |
|
33 // User includes |
|
34 #include "testwindow_win32.h" |
|
35 #include "radioapplication.h" |
|
36 #include "radiologger.h" |
|
37 #include "radio_global.h" |
|
38 #include "radioenginewrapper_win32_p.h" |
|
39 |
|
40 const int WINDOW_WIDTH = 360; |
|
41 const int WINDOW_HEIGHT = 640; |
|
42 const int TOOLBAR_HEIGHT = 120; |
|
43 const int WINDOW_EXTRA_WIDTH = 5; |
|
44 |
|
45 const QString KBtnDisconnectHeadset = "Disconnect Headset"; |
|
46 const QString KBtnConnectHeadset = "Connect Headset"; |
|
47 |
|
48 const QString KBtnGoOffline = "Go Offline"; |
|
49 const QString KBtnGoOnline = "Go Online"; |
|
50 |
|
51 struct Song |
|
52 { |
|
53 const char* mArtist; |
|
54 const char* mTitle; |
|
55 }; |
|
56 |
|
57 const Song KRecognizedSongs[] = { |
|
58 { "Red Hot Chili Peppers", "Under The Bridge" }, |
|
59 { "Queens Of The Stone Age", "No One Knows" }, |
|
60 { "The Presidents of the United States of America", "Dune Buggy" }, |
|
61 { "System of a Down", "Aerials" }, |
|
62 { "The White Stripes", "Seven Nation Army" }, |
|
63 { "Alice In Chains", "When The Sun Rose Again" }, |
|
64 { "Bullet For My Valentine", "Tears Don't Fall" } |
|
65 }; |
|
66 const int KSongsCount = sizeof( KRecognizedSongs ) / sizeof( KRecognizedSongs[0] ); |
|
67 |
|
68 /*! |
|
69 * |
|
70 */ |
|
71 Win32Window::Win32Window() : |
|
72 QWidget( 0 ), |
|
73 mOrientationButton( new QPushButton( "Change Orientation", this ) ), |
|
74 mVolUpButton( new QPushButton( "Volume Up", this ) ), |
|
75 mVolDownButton( new QPushButton( "Volume Down", this ) ), |
|
76 mAddSongButton( new QPushButton( "Add Song", this ) ), |
|
77 mClearSongButton( new QPushButton( "Clear Song", this ) ), |
|
78 mHeadsetButton( new QPushButton( KBtnDisconnectHeadset, this ) ), |
|
79 mHeadsetConnected( true ), |
|
80 mOfflineButton( new QPushButton( KBtnGoOffline, this ) ), |
|
81 mThemeBox( new QComboBox( this ) ), |
|
82 mToolbarLayout( 0 ), |
|
83 mVolume( 5 ), |
|
84 mRadioWindow( 0 ), |
|
85 mOrientation( Qt::Vertical ), |
|
86 mSongIndex( 0 ) |
|
87 { |
|
88 mThemeBox->setEditable( false ); |
|
89 initThemes(); |
|
90 |
|
91 Radio::connect( mOrientationButton, SIGNAL(clicked()), this, SLOT(changeOrientation()) ); |
|
92 Radio::connect( mVolUpButton, SIGNAL(clicked()), this, SLOT(volumeUp()) ); |
|
93 Radio::connect( mVolDownButton, SIGNAL(clicked()), this, SLOT(volumeDown()) ); |
|
94 Radio::connect( mHeadsetButton, SIGNAL(clicked()), this, SLOT(toggleHeadsetStatus()) ); |
|
95 Radio::connect( mAddSongButton, SIGNAL(clicked()), this, SLOT(addSong()) ); |
|
96 Radio::connect( mClearSongButton, SIGNAL(clicked()), this, SLOT(clearSong()) ); |
|
97 Radio::connect( mOfflineButton, SIGNAL(clicked()), this, SLOT(toggleOffline()) ); |
|
98 Radio::connect( mThemeBox, SIGNAL(activated(QString)), this, SLOT(changeTheme(QString)) ); |
|
99 |
|
100 QTimer::singleShot( 0, this, SLOT(updateWindowSize()) ); |
|
101 } |
|
102 |
|
103 /*! |
|
104 * |
|
105 */ |
|
106 Win32Window::~Win32Window() |
|
107 { |
|
108 } |
|
109 |
|
110 /*! |
|
111 * |
|
112 */ |
|
113 void Win32Window::addHbWindow( HbMainWindow* radioWindow ) |
|
114 { |
|
115 mRadioWindow = radioWindow; |
|
116 mOrientation = mRadioWindow->orientation(); |
|
117 updateWindowSize(); |
|
118 |
|
119 QVBoxLayout* layout = new QVBoxLayout( this ); |
|
120 layout->setMargin( 5 ); |
|
121 layout->setSpacing( 5 ); |
|
122 |
|
123 mToolbarLayout = new QGridLayout( this ); |
|
124 mToolbarLayout->setHorizontalSpacing( 5 ); |
|
125 mToolbarLayout->setVerticalSpacing( 5 ); |
|
126 |
|
127 mToolbarLayout->addWidget( mOrientationButton, 0, 0 ); |
|
128 mToolbarLayout->addWidget( mVolUpButton, 0, 1 ); |
|
129 mToolbarLayout->addWidget( mVolDownButton, 1, 1 ); |
|
130 mToolbarLayout->addWidget( mHeadsetButton, 1, 0 ); |
|
131 mToolbarLayout->addWidget( mAddSongButton, 2, 0 ); |
|
132 mToolbarLayout->addWidget( mClearSongButton, 2, 1 ); |
|
133 mToolbarLayout->addWidget( mOfflineButton, 3, 0 ); |
|
134 |
|
135 QGridLayout* themeLayout = new QGridLayout( this ); |
|
136 themeLayout->addWidget( new QLabel( "Theme:", this ), 0, 0 ); |
|
137 themeLayout->addWidget( mThemeBox, 0, 1 ); |
|
138 themeLayout->setColumnStretch( 1, 2 ); |
|
139 |
|
140 mToolbarLayout->addLayout( themeLayout, 3, 1 ); |
|
141 mToolbarLayout->setColumnStretch( 0, 1 ); |
|
142 mToolbarLayout->setColumnStretch( 1, 1 ); |
|
143 |
|
144 layout->addItem( mToolbarLayout ); |
|
145 layout->addWidget( radioWindow ); |
|
146 |
|
147 setLayout( layout ); |
|
148 } |
|
149 |
|
150 /*! |
|
151 * |
|
152 */ |
|
153 void Win32Window::init() |
|
154 { |
|
155 RadioEngineWrapperPrivate* wrapper = RadioEngineWrapperPrivate::instance(); |
|
156 if ( wrapper ) { |
|
157 QString error = wrapper->dataParsingError(); |
|
158 if ( !error.isEmpty() ) { |
|
159 QMessageBox msg( QMessageBox::Warning, "Unable to parse radio settings", error, QMessageBox::Ok ); |
|
160 msg.exec(); |
|
161 } |
|
162 |
|
163 if ( wrapper->isOffline() ) { |
|
164 mOfflineButton->setText( KBtnGoOnline ); |
|
165 } |
|
166 } |
|
167 updateWindowSize(); |
|
168 } |
|
169 |
|
170 /*! |
|
171 * Private slot |
|
172 */ |
|
173 void Win32Window::changeOrientation() |
|
174 { |
|
175 if ( mOrientation == Qt::Horizontal ) { |
|
176 mOrientation = Qt::Vertical; |
|
177 } else { |
|
178 mOrientation = Qt::Horizontal; |
|
179 } |
|
180 |
|
181 mRadioWindow->setOrientation( mOrientation ); |
|
182 updateWindowSize(); |
|
183 } |
|
184 |
|
185 /*! |
|
186 * Private slot |
|
187 */ |
|
188 void Win32Window::volumeUp() |
|
189 { |
|
190 if ( ++mVolume > MAXIMUM_VOLUME_LEVEL ) { |
|
191 mVolume = MAXIMUM_VOLUME_LEVEL; |
|
192 } |
|
193 RadioEngineWrapperPrivate::instance()->setVolume( mVolume ); |
|
194 } |
|
195 |
|
196 /*! |
|
197 * Private slot |
|
198 */ |
|
199 void Win32Window::volumeDown() |
|
200 { |
|
201 if ( --mVolume < 0 ) { |
|
202 mVolume = 0; |
|
203 } |
|
204 RadioEngineWrapperPrivate::instance()->setVolume( mVolume ); |
|
205 } |
|
206 |
|
207 /*! |
|
208 * Private slot |
|
209 */ |
|
210 void Win32Window::toggleHeadsetStatus() |
|
211 { |
|
212 mHeadsetConnected = !mHeadsetConnected; |
|
213 if ( mHeadsetConnected ) { |
|
214 mHeadsetButton->setText( KBtnDisconnectHeadset ); |
|
215 } else { |
|
216 mHeadsetButton->setText( KBtnConnectHeadset ); |
|
217 } |
|
218 RadioEngineWrapperPrivate::instance()->setHeadsetStatus( mHeadsetConnected ); |
|
219 } |
|
220 |
|
221 /*! |
|
222 * Private slot |
|
223 */ |
|
224 void Win32Window::updateWindowSize() |
|
225 { |
|
226 if ( mOrientation == Qt::Horizontal ) { |
|
227 resize( WINDOW_HEIGHT + WINDOW_EXTRA_WIDTH, WINDOW_WIDTH + TOOLBAR_HEIGHT ); |
|
228 } else { |
|
229 resize( WINDOW_WIDTH + WINDOW_EXTRA_WIDTH, WINDOW_HEIGHT + TOOLBAR_HEIGHT ); |
|
230 } |
|
231 } |
|
232 |
|
233 /*! |
|
234 * Private slot |
|
235 */ |
|
236 void Win32Window::addSong() |
|
237 { |
|
238 Song song = KRecognizedSongs[mSongIndex++]; |
|
239 mSongIndex %= KSongsCount; |
|
240 |
|
241 RadioEngineWrapperPrivate::instance()->addSong( song.mArtist, song.mTitle ); |
|
242 } |
|
243 |
|
244 /*! |
|
245 * Private slot |
|
246 */ |
|
247 void Win32Window::clearSong() |
|
248 { |
|
249 RadioEngineWrapperPrivate::instance()->clearSong(); |
|
250 } |
|
251 |
|
252 /*! |
|
253 * Private slot |
|
254 */ |
|
255 void Win32Window::toggleOffline() |
|
256 { |
|
257 bool offline = !RadioEngineWrapperPrivate::instance()->isOffline(); |
|
258 RadioEngineWrapperPrivate::instance()->setOffline( offline ); |
|
259 if ( offline ) { |
|
260 mOfflineButton->setText( KBtnGoOnline ); |
|
261 } else { |
|
262 mOfflineButton->setText( KBtnGoOffline ); |
|
263 } |
|
264 } |
|
265 |
|
266 /*! |
|
267 * Private slot |
|
268 */ |
|
269 void Win32Window::changeTheme( const QString& theme ) |
|
270 { |
|
271 LOG_FORMAT( "Changing to theme %s", GETSTRING( theme ) ); |
|
272 QLocalSocket socket; |
|
273 socket.connectToServer( "hbthemeserver" ); |
|
274 if ( socket.waitForConnected( 3000 ) ) { |
|
275 QByteArray outputByteArray; |
|
276 QDataStream outputDataStream( &outputByteArray, QIODevice::WriteOnly ); |
|
277 outputDataStream << 4; // EThemeSelection from HbThemeServerRequest in hbthemecommon_p.h |
|
278 outputDataStream << theme; |
|
279 socket.write( outputByteArray ); |
|
280 socket.flush(); |
|
281 } |
|
282 } |
|
283 |
|
284 /*! |
|
285 * |
|
286 */ |
|
287 void Win32Window::initThemes() |
|
288 { |
|
289 QStringList themeList; |
|
290 foreach ( const QString& themeRootPath, themeRootPaths() ) { |
|
291 QDir dir( themeRootPath ) ; |
|
292 QStringList list = dir.entryList( QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name ); |
|
293 |
|
294 if ( list.contains( "themes", Qt::CaseSensitive ) ) { |
|
295 QDir root = themeRootPath; |
|
296 dir.setPath( root.path() + "/themes/icons/" ); |
|
297 QStringList iconthemeslist = dir.entryList( QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name ); |
|
298 foreach ( QString themefolder, iconthemeslist ) { |
|
299 QDir iconThemePath( root.path() + "/themes/icons/" + themefolder ); |
|
300 if ( iconThemePath.exists( "index.theme" ) ) { |
|
301 QSettings iniSetting( iconThemePath.path() + "/index.theme", QSettings::IniFormat ); |
|
302 iniSetting.beginGroup( "Icon Theme" ); |
|
303 QString hidden = iniSetting.value( "Hidden" ).toString(); |
|
304 QString name = iniSetting.value( "Name" ).toString(); |
|
305 iniSetting.endGroup(); |
|
306 if ( (hidden == "true") || ( hidden == "" ) || ( name != themefolder ) ) { |
|
307 iconthemeslist.removeOne( themefolder ); |
|
308 } |
|
309 } |
|
310 else { |
|
311 iconthemeslist.removeOne( themefolder ); |
|
312 } |
|
313 |
|
314 } |
|
315 |
|
316 themeList.append( iconthemeslist ); |
|
317 } |
|
318 } |
|
319 |
|
320 if ( themeList.count() == 0 ) { |
|
321 themeList.insert( 0, "hbdefault" ); //adding one default entry |
|
322 mThemeBox->setEnabled( false ); |
|
323 } |
|
324 |
|
325 mThemeBox->setModel( new QStringListModel( themeList, mThemeBox ) ); |
|
326 |
|
327 for ( int i = 0; i < themeList.count(); ++i ) { |
|
328 QString theme = themeList.at( i ); |
|
329 if ( theme == HbInstance::instance()->theme()->name() ) { |
|
330 mThemeBox->setCurrentIndex( i ); |
|
331 } |
|
332 } |
|
333 } |
|
334 |
|
335 /*! |
|
336 * |
|
337 */ |
|
338 QStringList Win32Window::themeRootPaths() |
|
339 { |
|
340 QStringList rootDirs; |
|
341 QString envDir = qgetenv( "HB_THEMES_DIR" ); |
|
342 if ( !envDir.isEmpty() ) { |
|
343 rootDirs << envDir; |
|
344 } |
|
345 |
|
346 QString resourcesDir = HB_RESOURCES_DIR; |
|
347 if ( resourcesDir.isEmpty() ) { |
|
348 resourcesDir = "/hb_dev/src/hbcore/resources"; |
|
349 } |
|
350 rootDirs << resourcesDir; |
|
351 |
|
352 return rootDirs; |
|
353 } |