qtmobility/examples/player/player.cpp
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
child 5 453da2cfceef
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
    48 #include <qmediaservice.h>
    48 #include <qmediaservice.h>
    49 #include <qmediaplaylist.h>
    49 #include <qmediaplaylist.h>
    50 
    50 
    51 #include <QtGui>
    51 #include <QtGui>
    52 
    52 
       
    53 #ifdef Q_OS_SYMBIAN
       
    54 #include <QtGui/QDialog>
       
    55 #include <QtGui/QLineEdit>
       
    56 #include <QtGui/QListWidget>
       
    57 #include <QtNetwork/QHttp>
       
    58 #include <QDomDocument>
       
    59 
       
    60 #include "mediakeysobserver.h"
       
    61 #endif
       
    62 
    53 Player::Player(QWidget *parent)
    63 Player::Player(QWidget *parent)
    54     : QWidget(parent)
    64     : QWidget(parent)
    55     , videoWidget(0)
    65     , videoWidget(0)
    56     , coverLabel(0)
    66     , coverLabel(0)
    57     , slider(0)
    67     , slider(0)
    58 #ifdef Q_OS_SYMBIAN    
    68 #ifdef Q_OS_SYMBIAN
    59     , mediaKeysObserver(0)
    69     , mediaKeysObserver(0)
    60     , playlistDialog(0)
    70     , playlistDialog(0)
       
    71     , toggleAspectRatio(0)
       
    72     , showYoutubeDialog(0)
       
    73     , youtubeDialog(0)
    61 #else
    74 #else
    62     , colorDialog(0)
    75     , colorDialog(0)
    63 #endif    
    76 #endif
    64 {
    77 {
    65     player = new QMediaPlayer(this);
    78     player = new QMediaPlayer(this);
    66     playlist = new QMediaPlaylist(this);
    79     // owerd by PlaylistModel
       
    80     playlist = new QMediaPlaylist();
    67     playlist->setMediaObject(player);
    81     playlist->setMediaObject(player);
    68 
    82 
    69     connect(player, SIGNAL(durationChanged(qint64)), SLOT(durationChanged(qint64)));
    83     connect(player, SIGNAL(durationChanged(qint64)), SLOT(durationChanged(qint64)));
    70     connect(player, SIGNAL(positionChanged(qint64)), SLOT(positionChanged(qint64)));
    84     connect(player, SIGNAL(positionChanged(qint64)), SLOT(positionChanged(qint64)));
    71     connect(player, SIGNAL(metaDataChanged()), SLOT(metaDataChanged()));
    85     connect(player, SIGNAL(metaDataChanged()), SLOT(metaDataChanged()));
    72     connect(playlist, SIGNAL(currentIndexChanged(int)), SLOT(playlistPositionChanged(int)));
    86     connect(playlist, SIGNAL(currentIndexChanged(int)), SLOT(playlistPositionChanged(int)));
    73     connect(player, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)),
    87     connect(player, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)),
    74             this, SLOT(statusChanged(QMediaPlayer::MediaStatus)));
    88             this, SLOT(statusChanged(QMediaPlayer::MediaStatus)));
    75     connect(player, SIGNAL(bufferStatusChanged(int)), this, SLOT(bufferingProgress(int)));
    89     connect(player, SIGNAL(bufferStatusChanged(int)), this, SLOT(bufferingProgress(int)));
    76 
    90     connect(player, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(displayErrorMessage()));
    77     videoWidget = new VideoWidget;
    91 
       
    92     videoWidget = new VideoWidget(this);
    78     videoWidget->setMediaObject(player);
    93     videoWidget->setMediaObject(player);
    79 
    94 
    80     playlistModel = new PlaylistModel(this);
    95     playlistModel = new PlaylistModel(this);
    81     playlistModel->setPlaylist(playlist);
    96     playlistModel->setPlaylist(playlist);
    82 
    97 
    83     playlistView = new QListView;
    98     playlistView = new QListView(this);
    84     playlistView->setModel(playlistModel);
    99     playlistView->setModel(playlistModel);
    85     playlistView->setCurrentIndex(playlistModel->index(playlist->currentIndex(), 0));
   100     playlistView->setCurrentIndex(playlistModel->index(playlist->currentIndex(), 0));
    86 
   101 
    87     connect(playlistView, SIGNAL(activated(QModelIndex)), this, SLOT(jump(QModelIndex)));
   102     connect(playlistView, SIGNAL(activated(QModelIndex)), this, SLOT(jump(QModelIndex)));
    88 
   103 
    89     slider = new QSlider(Qt::Horizontal);
   104     slider = new QSlider(Qt::Horizontal, this);
    90     slider->setRange(0, player->duration() / 1000);
   105     slider->setRange(0, player->duration() / 1000);
    91 
   106 
    92     connect(slider, SIGNAL(sliderMoved(int)), this, SLOT(seek(int)));
   107     connect(slider, SIGNAL(sliderMoved(int)), this, SLOT(seek(int)));
    93 
   108 
    94 #ifdef Q_OS_SYMBIAN
   109 #ifndef Q_OS_SYMBIAN
    95 #else
       
    96     QPushButton *openButton = new QPushButton(tr("Open"), this);
   110     QPushButton *openButton = new QPushButton(tr("Open"), this);
    97 
   111 
    98     connect(openButton, SIGNAL(clicked()), this, SLOT(open()));
   112     connect(openButton, SIGNAL(clicked()), this, SLOT(open()));
    99 
   113 #endif
   100 #endif
   114 
   101     PlayerControls *controls = new PlayerControls(this);
   115     PlayerControls *controls = new PlayerControls(this);
   102     controls->setState(player->state());
   116     controls->setState(player->state());
   103     controls->setVolume(player->volume());
   117     controls->setVolume(player->volume());
   104     controls->setMuted(controls->isMuted());
   118     controls->setMuted(controls->isMuted());
   105 
   119 
   106     connect(controls, SIGNAL(play()), player, SLOT(play()));
   120     connect(controls, SIGNAL(play()), player, SLOT(play()));
   107     connect(controls, SIGNAL(pause()), player, SLOT(pause()));
   121     connect(controls, SIGNAL(pause()), player, SLOT(pause()));
   108     connect(controls, SIGNAL(stop()), player, SLOT(stop()));
   122     connect(controls, SIGNAL(stop()), player, SLOT(stop()));
   109     connect(controls, SIGNAL(next()), playlist, SLOT(next()));
   123     connect(controls, SIGNAL(next()), playlist, SLOT(next()));
   110     connect(controls, SIGNAL(previous()), playlist, SLOT(previous()));
   124     connect(controls, SIGNAL(previous()), this, SLOT(previousClicked()));
   111     connect(controls, SIGNAL(changeVolume(int)), player, SLOT(setVolume(int)));
   125     connect(controls, SIGNAL(changeVolume(int)), player, SLOT(setVolume(int)));
   112     connect(controls, SIGNAL(changeMuting(bool)), player, SLOT(setMuted(bool)));
   126     connect(controls, SIGNAL(changeMuting(bool)), player, SLOT(setMuted(bool)));
   113     connect(controls, SIGNAL(changeRate(qreal)), player, SLOT(setPlaybackRate(qreal)));
   127     connect(controls, SIGNAL(changeRate(qreal)), player, SLOT(setPlaybackRate(qreal)));
       
   128 
       
   129     connect(controls, SIGNAL(stop()), videoWidget, SLOT(update()));
   114 
   130 
   115     connect(player, SIGNAL(stateChanged(QMediaPlayer::State)),
   131     connect(player, SIGNAL(stateChanged(QMediaPlayer::State)),
   116             controls, SLOT(setState(QMediaPlayer::State)));
   132             controls, SLOT(setState(QMediaPlayer::State)));
   117     connect(player, SIGNAL(volumeChanged(int)), controls, SLOT(setVolume(int)));
   133     connect(player, SIGNAL(volumeChanged(int)), controls, SLOT(setVolume(int)));
   118     connect(player, SIGNAL(mutedChanged(bool)), controls, SLOT(setMuted(bool)));
   134     connect(player, SIGNAL(mutedChanged(bool)), controls, SLOT(setMuted(bool)));
   119 
   135 
   120 #ifdef Q_OS_SYMBIAN
   136 #ifndef Q_OS_SYMBIAN
   121 #else
       
   122     QPushButton *fullScreenButton = new QPushButton(tr("FullScreen"), this);
   137     QPushButton *fullScreenButton = new QPushButton(tr("FullScreen"), this);
   123     fullScreenButton->setCheckable(true);
   138     fullScreenButton->setCheckable(true);
   124 
   139 
   125     if (videoWidget != 0) {
   140     if (videoWidget != 0) {
   126         connect(fullScreenButton, SIGNAL(clicked(bool)), videoWidget, SLOT(setFullScreen(bool)));
   141         connect(fullScreenButton, SIGNAL(clicked(bool)), videoWidget, SLOT(setFullScreen(bool)));
   128                 fullScreenButton, SLOT(setChecked(bool)));
   143                 fullScreenButton, SLOT(setChecked(bool)));
   129     } else {
   144     } else {
   130         fullScreenButton->setEnabled(false);
   145         fullScreenButton->setEnabled(false);
   131     }
   146     }
   132 
   147 
   133     QPushButton *colorButton = new QPushButton(tr("Color Options..."));
   148     QPushButton *colorButton = new QPushButton(tr("Color Options..."), this);
   134     if (videoWidget)
   149     if (videoWidget)
   135         connect(colorButton, SIGNAL(clicked()), this, SLOT(showColorDialog()));
   150         connect(colorButton, SIGNAL(clicked()), this, SLOT(showColorDialog()));
   136     else
   151     else
   137         colorButton->setEnabled(false);
   152         colorButton->setEnabled(false);
   138 
   153 
   139 #endif
   154 #endif
   140     
   155 
   141 #ifdef Q_OS_SYMBIAN
   156 #ifdef Q_OS_SYMBIAN
   142     // Set some sensible default volume.
   157     // Set some sensible default volume.
   143     player->setVolume(50);
   158     player->setVolume(50);
   144     
   159 
   145     QLabel *label = new QLabel(tr("Playlist"), this);
   160     QLabel *label = new QLabel(tr("Playlist"), this);
   146     QVBoxLayout *playlistDialogLayout = new QVBoxLayout;
   161     QVBoxLayout *playlistDialogLayout = new QVBoxLayout;
   147     playlistDialogLayout->addWidget(label);
   162     playlistDialogLayout->addWidget(label);
   148     playlistDialogLayout->addWidget(playlistView);
   163     playlistDialogLayout->addWidget(playlistView);
   149     playlistDialog = new QDialog(this);
   164     playlistDialog = new QDialog(this);
   150     playlistDialog->setWindowTitle(tr("Playlist"));
   165     playlistDialog->setWindowTitle(tr("Playlist"));
   151     playlistDialog->setLayout(playlistDialogLayout);
   166     playlistDialog->setLayout(playlistDialogLayout);
   152     playlistDialog->setContextMenuPolicy(Qt::NoContextMenu);
   167     playlistDialog->setContextMenuPolicy(Qt::NoContextMenu);
   153     
   168 
   154     QAction *close = new QAction(tr("Close"), this);
   169     QAction *close = new QAction(tr("Close"), this);
   155     close->setSoftKeyRole(QAction::NegativeSoftKey);
   170     close->setSoftKeyRole(QAction::NegativeSoftKey);
   156     playlistDialog->addAction(close);
   171     playlistDialog->addAction(close);
   157     
   172 
   158     mediaKeysObserver = new MediaKeysObserver(this);
   173     mediaKeysObserver = new MediaKeysObserver(this);
   159     
   174 
       
   175     coverLabel = new QLabel(this);
       
   176     coverLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
       
   177     coverLabel->setMinimumSize(1, 1);
       
   178     coverLabel->hide();
       
   179 
   160     slider->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Maximum);
   180     slider->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Maximum);
   161     slider->setMinimumSize(1, 1);
   181     slider->setMinimumSize(1, 1);
   162     
   182 
   163     connect(controls, SIGNAL(open()), this, SLOT(open()));
   183     connect(controls, SIGNAL(open()), this, SLOT(open()));
   164     connect(controls, SIGNAL(fullScreen(bool)), this, SLOT(handleFullScreen(bool)));
   184     connect(controls, SIGNAL(fullScreen(bool)), this, SLOT(handleFullScreen(bool)));
   165     connect(videoWidget, SIGNAL(fullScreenChanged(bool)), this, SLOT(handleFullScreen(bool)));
       
   166     connect(controls, SIGNAL(openPlayList()), this, SLOT(showPlayList()));
   185     connect(controls, SIGNAL(openPlayList()), this, SLOT(showPlayList()));
   167     connect(player, SIGNAL(stateChanged(QMediaPlayer::State)), this, SLOT(handleStateChange(QMediaPlayer::State)));
   186     connect(player, SIGNAL(stateChanged(QMediaPlayer::State)), this, SLOT(handleStateChange(QMediaPlayer::State)));
   168     connect(mediaKeysObserver, SIGNAL(mediaKeyPressed(MediaKeysObserver::MediaKeys)), this, SLOT(handleMediaKeyEvent(MediaKeysObserver::MediaKeys)));
   187     connect(mediaKeysObserver, SIGNAL(mediaKeyPressed(MediaKeysObserver::MediaKeys)), this, SLOT(handleMediaKeyEvent(MediaKeysObserver::MediaKeys)));
   169     connect(close, SIGNAL(triggered()), playlistDialog, SLOT(reject()));
   188     connect(close, SIGNAL(triggered()), playlistDialog, SLOT(reject()));
   170     
   189 
   171     QBoxLayout *layout = new QVBoxLayout;
   190     QBoxLayout *layout = new QVBoxLayout;
       
   191     layout->setMargin(0);
   172     layout->addWidget(videoWidget, 7);
   192     layout->addWidget(videoWidget, 7);
       
   193     layout->addWidget(coverLabel, 7);
   173     layout->addWidget(slider, 1);
   194     layout->addWidget(slider, 1);
   174     layout->addWidget(controls, 2);
   195     layout->addWidget(controls, 2);
       
   196 
       
   197     createMenus();
   175 #else
   198 #else
   176     QBoxLayout *displayLayout = new QHBoxLayout;
   199     QBoxLayout *displayLayout = new QHBoxLayout;
   177     if (videoWidget)
   200     if (videoWidget)
   178         displayLayout->addWidget(videoWidget, 2);
   201         displayLayout->addWidget(videoWidget, 2);
   179     else
   202     else
   191 
   214 
   192     QBoxLayout *layout = new QVBoxLayout;
   215     QBoxLayout *layout = new QVBoxLayout;
   193     layout->addLayout(displayLayout);
   216     layout->addLayout(displayLayout);
   194     layout->addWidget(slider);
   217     layout->addWidget(slider);
   195     layout->addLayout(controlLayout);
   218     layout->addLayout(controlLayout);
   196 #endif    
   219 #endif
   197 
   220 
   198     setLayout(layout);
   221     setLayout(layout);
   199 
   222 
   200     metaDataChanged();
   223     metaDataChanged();
   201 
   224 
   209     }
   232     }
   210 }
   233 }
   211 
   234 
   212 Player::~Player()
   235 Player::~Player()
   213 {
   236 {
   214 #ifdef Q_OS_SYMBIAN
       
   215     delete playlistDialog;
       
   216 #else    
       
   217     delete playlist;
       
   218 #endif    
       
   219     delete player;
       
   220 }
   237 }
   221 
   238 
   222 void Player::open()
   239 void Player::open()
   223 {
   240 {
   224     QStringList fileNames = QFileDialog::getOpenFileNames();
   241     QStringList fileNames = QFileDialog::getOpenFileNames();
   231     slider->setMaximum(duration / 1000);
   248     slider->setMaximum(duration / 1000);
   232 }
   249 }
   233 
   250 
   234 void Player::positionChanged(qint64 progress)
   251 void Player::positionChanged(qint64 progress)
   235 {
   252 {
   236     slider->setValue(progress / 1000);
   253     if (!slider->isSliderDown()) {
       
   254         slider->setValue(progress / 1000);
       
   255     }
   237 }
   256 }
   238 
   257 
   239 void Player::metaDataChanged()
   258 void Player::metaDataChanged()
   240 {
   259 {
       
   260 #ifdef Q_OS_SYMBIAN
       
   261     if (player->isMetaDataAvailable()) {
       
   262         setTrackInfo(QString("(%1/%2) %3 - %4")
       
   263                 .arg(playlist->currentIndex()+1)
       
   264                 .arg(playlist->mediaCount())
       
   265                 .arg(player->metaData(QtMedia::AlbumArtist).toString())
       
   266                 .arg(player->metaData(QtMedia::Title).toString()));
       
   267 
       
   268         if (!player->isVideoAvailable()) {
       
   269             QUrl uri = player->metaData(QtMedia::CoverArtUrlLarge).value<QUrl>();
       
   270             QPixmap pixmap = NULL;
       
   271 
       
   272             if (uri.isEmpty()) {
       
   273                 QVariant picture = player->extendedMetaData("attachedpicture");
       
   274                 // Load picture from metadata
       
   275                 if (!picture.isNull() && picture.canConvert<QByteArray>())
       
   276                     pixmap.loadFromData(picture.value<QByteArray>());
       
   277 
       
   278                 // Load some jpg from same dir as media
       
   279                 else {
       
   280                     QUrl url = player->media().canonicalUrl();
       
   281                     QString path = url.path();
       
   282                     path = path.mid(1,path.lastIndexOf("/"));
       
   283                     QRegExp rx("*.jpg");
       
   284                     rx.setCaseSensitivity(Qt::CaseInsensitive);
       
   285                     rx.setPatternSyntax(QRegExp::Wildcard);
       
   286                     QDir directory(path);
       
   287                     QStringList allFiles = directory.entryList(QDir::Files | QDir::NoSymLinks);
       
   288 
       
   289                     foreach (QString file, allFiles)
       
   290                         if (rx.exactMatch(file)) {
       
   291                             path.append(file);
       
   292                             break;
       
   293                         }
       
   294                    pixmap.load(path);
       
   295                 }
       
   296                 // Load picture from file pointed by uri
       
   297             } else
       
   298                 pixmap.load(uri.toString());
       
   299 
       
   300             coverLabel->setPixmap((!pixmap.isNull())?pixmap:QPixmap());
       
   301             }
       
   302     hideOrShowCoverArt();
       
   303     }
       
   304 #else
   241     //qDebug() << "update metadata" << player->metaData(QtMedia::Title).toString();
   305     //qDebug() << "update metadata" << player->metaData(QtMedia::Title).toString();
   242     if (player->isMetaDataAvailable()) {
   306     if (player->isMetaDataAvailable()) {
   243         setTrackInfo(QString("%1 - %2")
   307         setTrackInfo(QString("%1 - %2")
   244                 .arg(player->metaData(QtMedia::AlbumArtist).toString())
   308                 .arg(player->metaData(QtMedia::AlbumArtist).toString())
   245                 .arg(player->metaData(QtMedia::Title).toString()));
   309                 .arg(player->metaData(QtMedia::Title).toString()));
   250             coverLabel->setPixmap(!url.isEmpty()
   314             coverLabel->setPixmap(!url.isEmpty()
   251                     ? QPixmap(url.toString())
   315                     ? QPixmap(url.toString())
   252                     : QPixmap());
   316                     : QPixmap());
   253         }
   317         }
   254     }
   318     }
       
   319 #endif
       
   320 }
       
   321 
       
   322 void Player::previousClicked()
       
   323 {
       
   324     // Go to previous track if we are within the first 5 seconds of playback
       
   325     // Otherwise, seek to the beginning.
       
   326     if(player->position() <= 5000)
       
   327         playlist->previous();
       
   328     else
       
   329         player->setPosition(0);
   255 }
   330 }
   256 
   331 
   257 void Player::jump(const QModelIndex &index)
   332 void Player::jump(const QModelIndex &index)
   258 {
   333 {
   259 #ifdef Q_OS_SYMBIAN
   334 #ifdef Q_OS_SYMBIAN
   276     player->setPosition(seconds * 1000);
   351     player->setPosition(seconds * 1000);
   277 }
   352 }
   278 
   353 
   279 void Player::statusChanged(QMediaPlayer::MediaStatus status)
   354 void Player::statusChanged(QMediaPlayer::MediaStatus status)
   280 {
   355 {
       
   356     handleCursor(status);
       
   357 
       
   358     // handle status message
   281     switch (status) {
   359     switch (status) {
   282     case QMediaPlayer::UnknownMediaStatus:
   360     case QMediaPlayer::UnknownMediaStatus:
   283     case QMediaPlayer::NoMedia:
   361     case QMediaPlayer::NoMedia:
   284     case QMediaPlayer::LoadedMedia:
   362     case QMediaPlayer::LoadedMedia:
   285     case QMediaPlayer::BufferingMedia:
   363     case QMediaPlayer::BufferingMedia:
   286     case QMediaPlayer::BufferedMedia:
   364     case QMediaPlayer::BufferedMedia:
   287 #ifndef QT_NO_CURSOR
       
   288         unsetCursor();
       
   289 #endif
       
   290         setStatusInfo(QString());
   365         setStatusInfo(QString());
   291         break;
   366         break;
   292     case QMediaPlayer::LoadingMedia:
   367     case QMediaPlayer::LoadingMedia:
   293 #ifndef QT_NO_CURSOR
       
   294         setCursor(QCursor(Qt::BusyCursor));
       
   295 #endif
       
   296         setStatusInfo(tr("Loading..."));
   368         setStatusInfo(tr("Loading..."));
   297         break;
   369         break;
   298     case QMediaPlayer::StalledMedia:
   370     case QMediaPlayer::StalledMedia:
   299 #ifndef QT_NO_CURSOR
   371         setStatusInfo(tr("Media Stalled"));
   300         setCursor(QCursor(Qt::BusyCursor));
       
   301 #endif
       
   302         break;
   372         break;
   303     case QMediaPlayer::EndOfMedia:
   373     case QMediaPlayer::EndOfMedia:
   304 #ifndef QT_NO_CURSOR
       
   305         unsetCursor();
       
   306 #endif
       
   307         setStatusInfo(QString());
       
   308         QApplication::alert(this);
   374         QApplication::alert(this);
   309         break;
   375         break;
   310     case QMediaPlayer::InvalidMedia:
   376     case QMediaPlayer::InvalidMedia:
       
   377         displayErrorMessage();
       
   378         break;
       
   379     }
       
   380 }
       
   381 
       
   382 void Player::handleCursor(QMediaPlayer::MediaStatus status)
       
   383 {
   311 #ifndef QT_NO_CURSOR
   384 #ifndef QT_NO_CURSOR
       
   385     if( status == QMediaPlayer::LoadingMedia ||
       
   386         status == QMediaPlayer::BufferingMedia ||
       
   387         status == QMediaPlayer::StalledMedia)
       
   388         setCursor(QCursor(Qt::BusyCursor));
       
   389     else
   312         unsetCursor();
   390         unsetCursor();
   313 #endif
   391 #endif
   314         setStatusInfo(player->errorString());
       
   315         break;
       
   316     }
       
   317 }
   392 }
   318 
   393 
   319 void Player::bufferingProgress(int progress)
   394 void Player::bufferingProgress(int progress)
   320 {
   395 {
   321     setStatusInfo(tr("Buffering %4%").arg(progress));
   396     setStatusInfo(tr("Buffering %4%").arg(progress));
   322 }
   397 }
   323 
   398 
   324 void Player::setTrackInfo(const QString &info)
   399 void Player::setTrackInfo(const QString &info)
   325 {
   400 {
   326     trackInfo = info;
   401     trackInfo = info;
   327 
   402 #ifdef Q_OS_SYMBIAN
       
   403     QMainWindow *main = qobject_cast<QMainWindow *>(this->parent());
       
   404     if (!statusInfo.isEmpty())
       
   405         main->setWindowTitle(QString("%1 | %2").arg(trackInfo).arg(statusInfo));
       
   406     else
       
   407         main->setWindowTitle(trackInfo);
       
   408 #else
   328     if (!statusInfo.isEmpty())
   409     if (!statusInfo.isEmpty())
   329         setWindowTitle(QString("%1 | %2").arg(trackInfo).arg(statusInfo));
   410         setWindowTitle(QString("%1 | %2").arg(trackInfo).arg(statusInfo));
   330     else
   411     else
   331         setWindowTitle(trackInfo);
   412         setWindowTitle(trackInfo);
   332 
   413 #endif
   333 }
   414 }
   334 
   415 
   335 void Player::setStatusInfo(const QString &info)
   416 void Player::setStatusInfo(const QString &info)
   336 {
   417 {
   337     statusInfo = info;
   418     statusInfo = info;
   338 
   419 #ifdef Q_OS_SYMBIAN
       
   420     QMainWindow *main = qobject_cast<QMainWindow *>(this->parent());
       
   421     if (!statusInfo.isEmpty())
       
   422         main->setWindowTitle(QString("%1 | %2").arg(trackInfo).arg(statusInfo));
       
   423     else
       
   424         main->setWindowTitle(trackInfo);
       
   425 #else
   339     if (!statusInfo.isEmpty())
   426     if (!statusInfo.isEmpty())
   340         setWindowTitle(QString("%1 | %2").arg(trackInfo).arg(statusInfo));
   427         setWindowTitle(QString("%1 | %2").arg(trackInfo).arg(statusInfo));
   341     else
   428     else
   342         setWindowTitle(trackInfo);
   429         setWindowTitle(trackInfo);
   343 }
   430 #endif
   344 #ifdef Q_OS_SYMBIAN
   431 }
       
   432 
       
   433 void Player::displayErrorMessage()
       
   434 {
       
   435 #ifdef Q_OS_SYMBIAN
       
   436     if(player->error()!=QMediaPlayer::NoError)
       
   437         QMessageBox::critical(NULL, tr("Error"), player->errorString(), QMessageBox::Ok);
   345 #else
   438 #else
       
   439     setStatusInfo(player->errorString());
       
   440 #endif
       
   441 
       
   442 
       
   443 }
       
   444 
       
   445 #ifndef Q_OS_SYMBIAN
   346 void Player::showColorDialog()
   446 void Player::showColorDialog()
   347 {
   447 {
   348     if (!colorDialog) {
   448     if (!colorDialog) {
   349         QSlider *brightnessSlider = new QSlider(Qt::Horizontal);
   449         QSlider *brightnessSlider = new QSlider(Qt::Horizontal);
   350         brightnessSlider->setRange(-100, 100);
   450         brightnessSlider->setRange(-100, 100);
   382     }
   482     }
   383     colorDialog->show();
   483     colorDialog->show();
   384 }
   484 }
   385 #endif
   485 #endif
   386 #ifdef Q_OS_SYMBIAN
   486 #ifdef Q_OS_SYMBIAN
       
   487 void Player::createMenus()
       
   488 {
       
   489     toggleAspectRatio = new QAction(tr("Ignore Aspect Ratio"), this);
       
   490     toggleAspectRatio->setCheckable(true);
       
   491     qobject_cast<QMainWindow *>(this->parent())->menuBar()->addAction(toggleAspectRatio);
       
   492     connect(toggleAspectRatio, SIGNAL(toggled(bool)), this, SLOT(handleAspectRatio(bool)));
       
   493 
       
   494     showYoutubeDialog = new QAction(tr("Youtube Search"), this);
       
   495     qobject_cast<QMainWindow *>(this->parent())->menuBar()->addAction(showYoutubeDialog);
       
   496     connect(showYoutubeDialog, SIGNAL(triggered()), this, SLOT(launchYoutubeDialog()));
       
   497 }
       
   498 
   387 void Player::handleFullScreen(bool isFullscreen)
   499 void Player::handleFullScreen(bool isFullscreen)
   388 {
   500 {
       
   501     QMainWindow* mainWindow = qobject_cast<QMainWindow *>(this->parent());
   389     if(isFullscreen) {
   502     if(isFullscreen) {
   390         showFullScreen();
   503         if(player->state()==QMediaPlayer::StoppedState)
   391         if(player->state()==QMediaPlayer::PlayingState || 
   504             videoWidget->setFullScreen(false);
   392            player->state()==QMediaPlayer::PausedState)
   505         else
   393             videoWidget->setFullScreen(true);
   506             videoWidget->setFullScreen(true);
   394         else
   507 
   395             videoWidget->setFullScreen(false);
   508         qobject_cast<QMainWindow *>(this->parent())->showFullScreen();
   396         
       
   397     } else
   509     } else
   398         showMaximized();
   510         qobject_cast<QMainWindow *>(this->parent())->showMaximized();
       
   511 }
       
   512 
       
   513 void Player::handleAspectRatio(bool aspectRatio)
       
   514 {
       
   515     if(aspectRatio) {
       
   516         toggleAspectRatio->setText(tr("Keep Aspect Ratio"));
       
   517         videoWidget->setAspectRatioMode(Qt::IgnoreAspectRatio);
       
   518 
       
   519     } else {
       
   520         toggleAspectRatio->setText(tr("Ignore Aspect Ratio"));
       
   521         videoWidget->setAspectRatioMode(Qt::KeepAspectRatio);
       
   522     }
       
   523 }
       
   524 
       
   525 void Player::hideOrShowCoverArt()
       
   526 {
       
   527     if(player->isVideoAvailable()) {
       
   528         coverLabel->hide();
       
   529         videoWidget->show();
       
   530         videoWidget->repaint();
       
   531     } else {
       
   532         coverLabel->show();
       
   533         videoWidget->hide();
       
   534     }
   399 }
   535 }
   400 
   536 
   401 void Player::handleStateChange(QMediaPlayer::State state)
   537 void Player::handleStateChange(QMediaPlayer::State state)
   402 {
   538 {
   403     if (state == QMediaPlayer::PausedState)
   539     if (state == QMediaPlayer::PausedState)
   404         return;
   540         return;
   405     
   541 
   406     handleFullScreen(isFullScreen());
   542     handleFullScreen(qobject_cast<QMainWindow *>(this->parent())->isFullScreen());
   407 }
   543 }
   408 
   544 
   409 void Player::handleMediaKeyEvent(MediaKeysObserver::MediaKeys key)
   545 void Player::handleMediaKeyEvent(MediaKeysObserver::MediaKeys key)
   410 {
   546 {
   411     switch (key) {
   547     switch (key) {
   412         case MediaKeysObserver::EVolIncKey: 
   548         case MediaKeysObserver::EVolIncKey:
   413             player->setVolume(player->volume() + 10);
   549             player->setVolume(player->volume() + 10);
   414             break;
   550             break;
   415         case MediaKeysObserver::EVolDecKey:
   551         case MediaKeysObserver::EVolDecKey:
   416             player->setVolume(player->volume() - 10);
   552             player->setVolume(player->volume() - 10);
   417             break;
   553             break;
   421 }
   557 }
   422 void Player::showPlayList()
   558 void Player::showPlayList()
   423 {
   559 {
   424     if (!playlistDialog)
   560     if (!playlistDialog)
   425         return;
   561         return;
   426     
   562 
   427     playlistDialog->exec();
   563     playlistDialog->exec();
   428 }
   564 }
   429 #endif
   565 
       
   566 void Player::launchYoutubeDialog()
       
   567 {
       
   568     if(!youtubeDialog)  {
       
   569         youtubeDialog = new QDialog(this);
       
   570 
       
   571         QLineEdit *input= new QLineEdit(youtubeDialog);
       
   572         QPushButton *searchButton = new QPushButton("Search", youtubeDialog);
       
   573         QListWidget *resultList = new QListWidget(youtubeDialog);
       
   574         QAction *add = new QAction(tr("Add"), youtubeDialog);
       
   575         QAction *close = new QAction(tr("Close"), youtubeDialog);
       
   576 
       
   577         add->setSoftKeyRole(QAction::PositiveSoftKey);
       
   578         close->setSoftKeyRole(QAction::NegativeSoftKey);
       
   579         youtubeDialog->addAction(add);
       
   580         youtubeDialog->addAction(close);
       
   581 
       
   582         connect(searchButton, SIGNAL(clicked()), this, SLOT(searchYoutubeVideo()));
       
   583         connect(add, SIGNAL(triggered()), this, SLOT(addYoutubeVideo()));
       
   584         connect(close, SIGNAL(triggered()), youtubeDialog, SLOT(close()));
       
   585         connect(&http, SIGNAL(requestFinished(int, bool)), this, SLOT(youtubeHttpRequestFinished(int, bool)));
       
   586         connect(&http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader&)), this, SLOT(youtubeReadResponseHeader(const QHttpResponseHeader&)));
       
   587 
       
   588         QHBoxLayout *topLayout = new QHBoxLayout;
       
   589         topLayout->addWidget(input);
       
   590         topLayout->addWidget(searchButton);
       
   591 
       
   592         QVBoxLayout *mainLayout = new QVBoxLayout;
       
   593         mainLayout->addLayout(topLayout);
       
   594         mainLayout->addWidget(resultList);
       
   595         youtubeDialog->setLayout(mainLayout);
       
   596     }
       
   597     youtubeDialog->showMaximized();
       
   598 }
       
   599 
       
   600 void Player::youtubeReadResponseHeader(const QHttpResponseHeader& responseHeader)
       
   601 {
       
   602     switch (responseHeader.statusCode())
       
   603     {
       
   604         case 200:   // Ok
       
   605         case 301:   // Moved Permanently
       
   606         case 302:   // Found
       
   607         case 303:   // See Other
       
   608         case 307:   // Temporary Redirect
       
   609             // these are not error conditions
       
   610             break;
       
   611         default: {
       
   612             http.abort();
       
   613             QMessageBox::critical(NULL, tr("Error"), tr("Download failed: %1.").arg(responseHeader.reasonPhrase()));
       
   614             break;
       
   615         }
       
   616     }
       
   617 }
       
   618 
       
   619 void Player::addYoutubeVideo()
       
   620 {
       
   621     if(!youtubeDialog)
       
   622         return;
       
   623 
       
   624     QListWidget *resultList = youtubeDialog->findChild<QListWidget *>();
       
   625     if(!resultList || resultList->count() == 0)
       
   626         return;
       
   627 
       
   628     playlist->addMedia(resultList->currentItem()->data(Qt::UserRole).toUrl());
       
   629 }
       
   630 
       
   631 void Player::searchYoutubeVideo()
       
   632 {
       
   633     if(!youtubeDialog)
       
   634         return;
       
   635 
       
   636     QLineEdit *input = youtubeDialog->findChild<QLineEdit *>();
       
   637     QListWidget *resultList = youtubeDialog->findChild<QListWidget *>();
       
   638     QString urlstring = QString("http://gdata.youtube.com/feeds/api/videos?q=%1&max-results=25&v=2&format=6").arg(input->text().replace(' ', '+'));
       
   639     QUrl url(urlstring);
       
   640     http.setHost(url.host(), QHttp::ConnectionModeHttp, url.port() == -1 ? 0 : url.port());
       
   641     resultList->clear();
       
   642     httpGetId = http.get(urlstring);
       
   643 }
       
   644 
       
   645 void Player::youtubeHttpRequestFinished(int requestId, bool error)
       
   646 {
       
   647     if(!youtubeDialog || requestId != httpGetId)
       
   648         return;
       
   649 
       
   650     if (error) {
       
   651         QMessageBox::critical(NULL, tr("Error"), tr("Download failed: %1.").arg(http.errorString()));
       
   652         return;
       
   653     }
       
   654 
       
   655     QTemporaryFile file;
       
   656     if (!file.open()) {
       
   657         QMessageBox::critical(NULL, tr("Error"), tr("Could not open temporary file"));
       
   658         return;
       
   659     }
       
   660 
       
   661     QString data = http.readAll();
       
   662     QTextStream out(&file);
       
   663     out << data;
       
   664     file.close();
       
   665 
       
   666     QDomDocument domDocument;
       
   667     QString errorMessage;
       
   668     if (!domDocument.setContent(&file, true, &errorMessage)) {
       
   669         QMessageBox::critical(NULL, tr("Error"), errorMessage);
       
   670         return;
       
   671     }
       
   672 
       
   673     QDomElement root = domDocument.documentElement();
       
   674     if (root.tagName() != "feed")
       
   675         return;
       
   676 
       
   677     QListWidget *resultList = youtubeDialog->findChild<QListWidget *>();
       
   678     QDomElement entryElement = root.firstChildElement("entry");
       
   679     while(!entryElement.isNull())
       
   680     {
       
   681         QString title = entryElement.firstChildElement("title").text();
       
   682         QDomElement groupElement = entryElement.firstChildElement("group");
       
   683         QDomElement incidentElement2 = groupElement.firstChildElement("content");
       
   684         while(!incidentElement2.isNull())
       
   685         {
       
   686             // "6" = MPEG-4 SP video (up to 176x144) and AAC audio.
       
   687             if (incidentElement2.attribute("format") == "6") {
       
   688                 QListWidgetItem* item = new QListWidgetItem(title, resultList);
       
   689                 item->setData(Qt::UserRole, incidentElement2.attribute("url"));
       
   690                 break;
       
   691             }
       
   692             incidentElement2 = incidentElement2.nextSiblingElement("content");
       
   693         }
       
   694         entryElement = entryElement.nextSiblingElement("entry");
       
   695     }
       
   696 }
       
   697 #endif