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