Mixxx

/home/maxime/Projets/Mixxx/1.10/mixxx/src/library/bundledsongswebview.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           bundledsongswebview.cpp
00003                              -------------------
00004     begin                : Jan 2010
00005     copyright            : (C) 2010 Albert Santoni
00006     email                : alberts@mixxx.org
00007 ***************************************************************************/
00008 
00009 /***************************************************************************
00010 *                                                                         *
00011 *   This program is free software; you can redistribute it and/or modify  *
00012 *   it under the terms of the GNU General Public License as published by  *
00013 *   the Free Software Foundation; either version 2 of the License, or     *
00014 *   (at your option) any later version.                                   *
00015 *                                                                         *
00016 ***************************************************************************/
00017 
00018 #include <QtXml>
00019 #include <QDebug>
00020 #include <QDesktopServices>
00021 #include <QWebFrame>
00022 #include "library/trackcollection.h"
00023 #include "library/dao/trackdao.h"
00024 #include "bundledsongswebview.h"
00025 
00026 #define CONFIG_KEY "[Promo]"
00027 
00028 BundledSongsWebView::BundledSongsWebView(QWidget* parent,
00029                                          TrackCollection* trackCollection,
00030                                          QString promoBundlePath,
00031                                          QString localURL, bool firstRun,
00032                                          ConfigObject<ConfigValue>* config) :
00033     QWebView(parent),
00034     LibraryView(),
00035     m_pTrackCollection(trackCollection),
00036     m_bFirstRun(firstRun),
00037     m_pConfig(config)
00038 {
00039     m_sPromoBundlePath = promoBundlePath;
00040     m_sLocalURL = localURL;
00041     m_statTracking = (int)m_pConfig->getValueString(ConfigKey(CONFIG_KEY,"StatTracking")).toInt();
00042 
00043     //Disable right-click
00044     QWidget::setContextMenuPolicy(Qt::PreventContextMenu);
00045 
00046     //Hook up a bunch of signals to make this class exposed to the javascript
00047     //inside our HTML page.
00048     connect(page()->mainFrame(), SIGNAL(loadStarted()), this, SLOT(attachObjects()));
00049     attachObjects();
00050     connect(page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(attachObjects()) );
00051 
00052     //Load the promo tracks webpage
00053     QWebView::load(QUrl(m_sLocalURL));
00054 
00055     //Let us manually handle links that are clicked via the linkClicked()
00056     //signal...
00057     QWebPage* page = QWebView::page();
00058     page->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
00059 
00060     connect(this, SIGNAL(linkClicked(const QUrl&)),
00061             this, SLOT(handleClickedLink(const QUrl&)));
00062     connect(this, SIGNAL(loadFinished(bool)),
00063             this, SLOT(loadFinished(bool)));
00064 }
00065 
00066 BundledSongsWebView::~BundledSongsWebView()
00067 {
00068 
00069 }
00070 
00071 void BundledSongsWebView::attachObjects()
00072 {
00073     //qDebug() << "attachObjects()";
00074     page()->mainFrame()->addToJavaScriptWindowObject("mixxx", this);
00075 }
00076 
00077 void BundledSongsWebView::setup(QDomNode node)
00078 {
00079 
00080 }
00081 
00082 void BundledSongsWebView::loadFinished(bool ok)
00083 {
00084     if (m_bFirstRun)
00085         page()->mainFrame()->evaluateJavaScript("splash();");
00086 }
00087 
00088 void BundledSongsWebView::onShow()
00089 {
00090     //qDebug() << ">>>>>>BundledSongsWebView::onShow()";
00091     //Trigger the splash() function that's defined in our HTML page's javascript
00092     //Qt rocks!
00093     //if (firstRun())
00094     //    page()->mainFrame()->evaluateJavaScript("splash();");
00095     //else
00096     //    page()->mainFrame()->evaluateJavaScript("showMainStuff(0, 0);");
00097 }
00098 
00099 /* Google Analytics doesn't like our crappy malformed "Mixxx 1.8" string
00100    as a user agent. Let Qt construct it for us instead by leaving this commented out.
00101 QString PromoTracksWebView::userAgentForUrl (const QUrl & url) const
00102 {
00103     return QCoreApplication::applicationName() + " " + QCoreApplication::applicationVersion();
00104 } */
00105 
00106 void BundledSongsWebView::handleClickedLink(const QUrl& url)
00107 {
00108     //qDebug() << "link clicked!" << url;
00109 
00110     if (url.scheme().startsWith("deck"))
00111     {
00112         QString location = m_sPromoBundlePath + "/" + url.path();
00113         QFileInfo fileInfo(location);
00114         location = fileInfo.absoluteFilePath();
00115 
00116         // Try to get TrackInfoObject* from library, identified by location.
00117         TrackDAO& trackDao = m_pTrackCollection->getTrackDAO();
00118         TrackPointer pTrack = trackDao.getTrack(trackDao.getTrackId(location));
00119         // If not, create a new TrackInfoObject*
00120         if (pTrack == NULL)
00121         {
00122             qDebug () << "Didn't find promo track in the library";
00123             pTrack = TrackPointer(new TrackInfoObject(location), &QObject::deleteLater);
00124             //Let's immediately save the track so that the FIXME
00125             trackDao.saveTrack(pTrack);
00126         }
00127 
00128         if (url.scheme() == "deck1")
00129         {
00130             emit(loadTrackToPlayer(pTrack, "[Channel1]"));
00131         }
00132         else if (url.scheme() == "deck2")
00133         {
00134             emit(loadTrackToPlayer(pTrack, "[Channel2]"));
00135         }
00136     }
00137     else
00138     {
00139         QDesktopServices::openUrl(url);
00140     }
00141     //emit(loadTrack(track));
00142     //int player = 1;
00143     //emit(loadTrackToPlayer(track, player));
00144 }
00145 
00146 //TODO: Implement this for MIDI control
00147 void BundledSongsWebView::keyPressEvent(QKeyEvent* event)
00148 {
00149     //Look at WTrackTableView::keyPressEvent(...) for some
00150     //code to start with...
00151 }
00152 
00153 bool BundledSongsWebView::statTracking() const
00154 {
00155     return m_statTracking;
00156 };
00157 
00158 void BundledSongsWebView::setStatTracking(bool statTracking)
00159 {
00160     //qDebug() << "setStatTracking" << statTracking;
00161     m_statTracking = statTracking;
00162     m_pConfig->set(ConfigKey(CONFIG_KEY,"StatTracking"), ConfigValue(m_statTracking));
00163 };
00164 
00165 
00166 bool BundledSongsWebView::firstRun() const
00167 {
00168     return m_bFirstRun;
00169 };
00170 
00171 void BundledSongsWebView::setFirstRun(bool firstRun)
00172 {
00173     m_bFirstRun = firstRun;
00174 };
00175 
00176 void BundledSongsWebView::loadSelectedTrack() {
00177     // Do nothing for now. The web view doesn't have the concept of a selection right now.
00178 }
00179 
00180 void BundledSongsWebView::loadSelectedTrackToGroup(QString group) {
00181     // Do nothing for now. The web view doesn't have the concept of a selection right now.
00182 }
00183 
00184 void BundledSongsWebView::moveSelection(int delta) {
00185     // Do nothing for now. The web view doesn't have the concept of a selection right now.
00186 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines