Mixxx

/home/maxime/Projets/Mixxx/1.10/mixxx/src/library/browse/browsefeature.cpp

Go to the documentation of this file.
00001 // browsefeature.cpp
00002 // Created 9/8/2009 by RJ Ryan (rryan@mit.edu)
00003 
00004 #include <QStringList>
00005 #include <QTreeView>
00006 #include <QDirModel>
00007 #include <QStringList>
00008 #include <QFileInfo>
00009 #include <QDesktopServices>
00010 
00011 #include "trackinfoobject.h"
00012 #include "library/treeitem.h"
00013 #include "library/browse/browsefeature.h"
00014 #include "library/trackcollection.h"
00015 #include "library/dao/trackdao.h"
00016 
00017 BrowseFeature::BrowseFeature(QObject* parent, ConfigObject<ConfigValue>* pConfig,
00018                              TrackCollection* pTrackCollection,
00019                              RecordingManager* pRecordingManager)
00020         : LibraryFeature(parent),
00021           m_pConfig(pConfig),
00022           m_browseModel(this, pTrackCollection, pRecordingManager),
00023           m_proxyModel(&m_browseModel),
00024           m_pTrackCollection(pTrackCollection) {
00025 
00026     m_proxyModel.setFilterCaseSensitivity(Qt::CaseInsensitive);
00027     m_proxyModel.setSortCaseSensitivity(Qt::CaseInsensitive);
00028 
00029     //The invisible root item of the child model
00030     TreeItem* rootItem = new TreeItem();
00031 
00032     TreeItem* quick_link = new TreeItem(tr("Quick Links"), QUICK_LINK_NODE ,this , rootItem);
00033     rootItem->appendChild(quick_link);
00034 
00035     //Create the 'devices' shortcut
00036 #if defined(__WINDOWS__)
00037     TreeItem* devices_link = new TreeItem(tr("Devices"), DEVICE_NODE ,this , rootItem);
00038     rootItem->appendChild(devices_link);
00039     //show drive letters
00040     QFileInfoList drives = QDir::drives();
00041     //show drive letters
00042     foreach(QFileInfo drive, drives){
00043         TreeItem* driveLetter = new TreeItem(
00044                         drive.canonicalPath(), // displays C:
00045                         drive.filePath(), //Displays C:/
00046                         this ,
00047                         devices_link);
00048         devices_link->appendChild(driveLetter);
00049     }
00050 #elif defined(__APPLE__)
00051     //Apple hides the base Linux file structure
00052     //But all devices are mounted at /Volumes
00053     TreeItem* devices_link = new TreeItem(tr("Devices"), "/Volumes/", this , rootItem);
00054     rootItem->appendChild(devices_link);
00055 #else //LINUX
00056     TreeItem* devices_link = new TreeItem(tr("Removable Devices"), "/media/", this , rootItem);
00057     rootItem->appendChild(devices_link);
00058 
00059     //show root directory on UNIX-based operating systems
00060     TreeItem* root_folder_item = new TreeItem(QDir::rootPath(), QDir::rootPath(),this , rootItem);
00061     rootItem->appendChild(root_folder_item);
00062 
00063 #endif
00064 
00065     /*
00066      * Just a word about how the TreeItem objects are used for the BrowseFeature:
00067      * The constructor has 4 arguments:
00068      * 1. argument represents the folder name shown in the sidebar later on
00069      * 2. argument represents the folder path which MUST end with '/'
00070      * 3. argument is the library feature itself
00071      * 4. the parent TreeItem object
00072      *
00073      * Except the invisible root item, you must always state all 4 arguments.
00074      *
00075      * Once the TreeItem objects are inserted to models, the models take care of their
00076      * deletion.
00077      */
00078 
00079     //Add a shortcut to the Music folder which Mixxx uses
00080     QString mixxx_music_dir = m_pConfig->getValueString(ConfigKey("[Playlist]","Directory"));
00081     QString os_music_folder_dir = QDesktopServices::storageLocation(QDesktopServices::MusicLocation);
00082     QString os_documents_folder_dir = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
00083     QString os_home_folder_dir = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
00084     QString os_desktop_folder_dir = QDesktopServices::storageLocation(QDesktopServices::DesktopLocation);
00085 
00086     TreeItem* mixxx_library_dir_item = new TreeItem(tr("Mixxx Library"), mixxx_music_dir +"/" ,this , quick_link);
00087     quick_link->appendChild(mixxx_library_dir_item);
00088 
00089     TreeItem*os_home_dir_item = new TreeItem(tr("Home"), os_home_folder_dir +"/" , this , quick_link);
00090     quick_link->appendChild(os_home_dir_item);
00091 
00092     TreeItem*os_music_dir_item = new TreeItem(tr("Music"), os_music_folder_dir +"/" , this , quick_link);
00093     quick_link->appendChild(os_music_dir_item);
00094 
00095     TreeItem*os_docs_dir_item = new TreeItem(tr("Documents"), os_documents_folder_dir +"/" , this , quick_link);
00096     quick_link->appendChild(os_docs_dir_item);
00097 
00098     TreeItem*os_desktop_dir_item = new TreeItem(tr("Desktop"), os_desktop_folder_dir +"/" , this , quick_link);
00099     quick_link->appendChild(os_desktop_dir_item);
00100 
00101     //initialize the model
00102     m_childModel.setRootItem(rootItem);
00103 }
00104 
00105 BrowseFeature::~BrowseFeature() {
00106 
00107 }
00108 
00109 QVariant BrowseFeature::title() {
00110     return QVariant(tr("Browse"));
00111 }
00112 
00113 QIcon BrowseFeature::getIcon() {
00114     return QIcon(":/images/library/ic_library_browse.png");
00115 }
00116 
00117 TreeItemModel* BrowseFeature::getChildModel() {
00118     return &m_childModel;
00119 }
00120 
00121 bool BrowseFeature::dropAccept(QUrl url) {
00122     Q_UNUSED(url);
00123     return false;
00124 }
00125 
00126 bool BrowseFeature::dropAcceptChild(const QModelIndex& index, QUrl url) {
00127     Q_UNUSED(index);
00128     Q_UNUSED(url);
00129     return false;
00130 }
00131 
00132 bool BrowseFeature::dragMoveAccept(QUrl url) {
00133     Q_UNUSED(url);
00134     return false;
00135 }
00136 
00137 bool BrowseFeature::dragMoveAcceptChild(const QModelIndex& index, QUrl url) {
00138     Q_UNUSED(index);
00139     Q_UNUSED(url);
00140     return false;
00141 }
00142 
00143 void BrowseFeature::activate() {
00144     emit(restoreSearch(m_currentSearch));
00145 }
00146 /*
00147  * Note: This is executed whenever you single click on an child item
00148  * Single clicks will not populate sub folders
00149  */
00150 void BrowseFeature::activateChild(const QModelIndex& index) {
00151     TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
00152     qDebug() << "BrowseFeature::activateChild " << item->data() << " " << item->dataPath();
00153     m_browseModel.setPath(item->dataPath().toString());
00154     emit(showTrackModel(&m_proxyModel));
00155 
00156 }
00157 
00158 void BrowseFeature::onRightClick(const QPoint& globalPos) {
00159     Q_UNUSED(globalPos);
00160 }
00161 
00162 void BrowseFeature::onRightClickChild(const QPoint& globalPos, QModelIndex index) {
00163     Q_UNUSED(globalPos);
00164     Q_UNUSED(index);
00165 }
00166 
00167 /*
00168  * This is called whenever you double click or use the triangle symbol to expand
00169  * the subtree. The method will read the subfolders.
00170  */
00171 void BrowseFeature::onLazyChildExpandation(const QModelIndex &index){
00172     TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
00173     qDebug() << "BrowseFeature::onLazyChildExpandation " << item->data() << " " << item->dataPath();
00174 
00175     // If the item is a build-in node, e.g., 'QuickLink' return
00176     if(item->dataPath().toString() == QUICK_LINK_NODE)
00177         return;
00178 
00179     //Before we populate the subtree, we need to delete old subtrees
00180    m_childModel.removeRows(0, item->childCount(), index);
00181 
00182     // List of subfolders or drive letters
00183     QList<TreeItem*> folders;
00184 
00185     // If we are on the special device node
00186     if(item->dataPath().toString() == DEVICE_NODE){
00187        //Repopulate drive list
00188         QFileInfoList drives = QDir::drives();
00189         //show drive letters
00190         foreach(QFileInfo drive, drives){
00191             TreeItem* driveLetter = new TreeItem(
00192                             drive.canonicalPath(), // displays C:
00193                             drive.filePath(), //Displays C:/
00194                             this ,
00195                             item);
00196             folders << driveLetter;
00197         }
00198 
00199     }
00200     else // we assume that the path refers to a folder in the file system
00201     {
00202         //populate childs
00203         QDir dir(item->dataPath().toString());
00204         QFileInfoList all = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);
00205 
00206         // loop through all the item and construct the childs
00207         foreach(QFileInfo one, all){
00208             //Skip folders that end with .app on OS X
00209 #if defined(__APPLE__)
00210             if (one.isDir() && one.fileName().endsWith(".app"))
00211                 continue;
00212 #endif
00213             // We here create new items for the sidebar models
00214             // Once the items are added to the TreeItemModel,
00215             // the models takes ownership of them and ensures their deletion
00216             TreeItem* folder = new TreeItem(one.fileName(),
00217                                             item->dataPath().toString().append(one.fileName() +"/"),
00218                                             this,
00219                                             item);
00220             folders << folder;
00221         }
00222     }
00223     //we need to check here if subfolders are found
00224     //On Ubuntu 10.04, otherwise, this will draw an icon although the folder has no subfolders
00225     if(!folders.isEmpty())
00226        m_childModel.insertRows(folders, 0, folders.size() , index);
00227 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines