Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2017
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.37 KB | None | 0 0
  1. /****************************************************************************************
  2. * Copyright (c) 2007 Maximilian Kossick <maximilian.kossick@googlemail.com> *
  3. * Copyright (c) 2007 Casey Link <unnamedrambler@gmail.com> *
  4. * Copyright (c) 2010 Jeff Mitchell <mitchell@kde.org> *
  5. * Copyright (c) 2013 Ralf Engels <ralf-engels@gmx.de> *
  6. * *
  7. * This program is free software; you can redistribute it and/or modify it under *
  8. * the terms of the GNU General Public License as published by the Free Software *
  9. * Foundation; either version 2 of the License, or (at your option) any later *
  10. * version. *
  11. * *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT ANY *
  13. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
  14. * PARTICULAR PURPOSE. See the GNU General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License along with *
  17. * this program. If not, see <http://www.gnu.org/licenses/>. *
  18. ****************************************************************************************/
  19.  
  20. #define DEBUG_PREFIX "DatabaseCollection"
  21.  
  22. #include "DatabaseCollection.h"
  23.  
  24. #include "core/support/Debug.h"
  25. #include "scanner/GenericScanManager.h"
  26. #include "MountPointManager.h"
  27.  
  28. using namespace Collections;
  29.  
  30. DatabaseCollection::DatabaseCollection()
  31. : Collection()
  32. , m_mpm( 0 )
  33. , m_scanManager( 0 )
  34. , m_blockUpdatedSignalCount( 0 )
  35. , m_updatedSignalRequested( false )
  36. {
  37. }
  38.  
  39. DatabaseCollection::~DatabaseCollection()
  40. {
  41. delete m_mpm;
  42. }
  43.  
  44. QString
  45. DatabaseCollection::collectionId() const
  46. {
  47. return QLatin1String( "localCollection" );
  48. }
  49.  
  50. QString
  51. DatabaseCollection::prettyName() const
  52. {
  53. return i18n( "Local Collection" );
  54. }
  55.  
  56. KIcon
  57. DatabaseCollection::icon() const
  58. {
  59. return KIcon("drive-harddisk");
  60. }
  61.  
  62. GenericScanManager*
  63. DatabaseCollection::scanManager() const
  64. {
  65. return m_scanManager;
  66. }
  67.  
  68. MountPointManager*
  69. DatabaseCollection::mountPointManager() const
  70. {
  71. Q_ASSERT( m_mpm );
  72. return m_mpm;
  73. }
  74.  
  75. void
  76. DatabaseCollection::setMountPointManager( MountPointManager *mpm )
  77. {
  78. Q_ASSERT( mpm );
  79.  
  80. if( m_mpm )
  81. {
  82. disconnect( mpm, SIGNAL(deviceAdded(int)), this, SLOT(slotDeviceAdded(int)) );
  83. disconnect( mpm, SIGNAL(deviceRemoved(int)), this, SLOT(slotDeviceRemoved(int)) );
  84. }
  85.  
  86. m_mpm = mpm;
  87. connect( mpm, SIGNAL(deviceAdded(int)), this, SLOT(slotDeviceAdded(int)) );
  88. connect( mpm, SIGNAL(deviceRemoved(int)), this, SLOT(slotDeviceRemoved(int)) );
  89. }
  90.  
  91.  
  92. QStringList
  93. DatabaseCollection::collectionFolders() const
  94. {
  95. return mountPointManager()->collectionFolders();
  96. }
  97.  
  98. void
  99. DatabaseCollection::setCollectionFolders( const QStringList &folders )
  100. {
  101. mountPointManager()->setCollectionFolders( folders );
  102. }
  103.  
  104.  
  105. void
  106. DatabaseCollection::blockUpdatedSignal()
  107. {
  108. QMutexLocker locker( &m_mutex );
  109. m_blockUpdatedSignalCount ++;
  110. }
  111.  
  112. void
  113. DatabaseCollection::unblockUpdatedSignal()
  114. {
  115. QMutexLocker locker( &m_mutex );
  116.  
  117. Q_ASSERT( m_blockUpdatedSignalCount > 0 );
  118. m_blockUpdatedSignalCount --;
  119.  
  120. // check if meanwhile somebody had updated the collection
  121. if( m_blockUpdatedSignalCount == 0 && m_updatedSignalRequested )
  122. {
  123. m_updatedSignalRequested = false;
  124. locker.unlock();
  125. emit updated();
  126. }
  127. }
  128.  
  129. void
  130. DatabaseCollection::collectionUpdated()
  131. {
  132. QMutexLocker locker( &m_mutex );
  133. if( m_blockUpdatedSignalCount == 0 )
  134. {
  135. m_updatedSignalRequested = false;
  136. locker.unlock();
  137. emit updated();
  138. }
  139. else
  140. {
  141. m_updatedSignalRequested = true;
  142. }
  143. }
  144.  
  145. bool
  146. DatabaseCollection::hasCapabilityInterface( Capabilities::Capability::Type type ) const
  147. {
  148. switch( type )
  149. {
  150. case Capabilities::Capability::CollectionImport:
  151. case Capabilities::Capability::CollectionScan:
  152. return true;
  153. default:
  154. return Collection::hasCapabilityInterface( type );
  155. }
  156. }
  157.  
  158. Capabilities::Capability*
  159. DatabaseCollection::createCapabilityInterface( Capabilities::Capability::Type type )
  160. {
  161. switch( type )
  162. {
  163. case Capabilities::Capability::CollectionImport:
  164. return new DatabaseCollectionImportCapability( this );
  165. case Capabilities::Capability::CollectionScan:
  166. return new DatabaseCollectionScanCapability( this );
  167. default:
  168. return Collection::createCapabilityInterface( type );
  169. }
  170. }
  171.  
  172. // --------- DatabaseCollectionScanCapability -------------
  173.  
  174. DatabaseCollectionScanCapability::DatabaseCollectionScanCapability( DatabaseCollection* collection )
  175. : m_collection( collection )
  176. {
  177. Q_ASSERT( m_collection );
  178. }
  179.  
  180. DatabaseCollectionScanCapability::~DatabaseCollectionScanCapability()
  181. { }
  182.  
  183. void
  184. DatabaseCollectionScanCapability::startFullScan()
  185. {
  186. QList<KUrl> urls;
  187. foreach( const QString& path, m_collection->mountPointManager()->collectionFolders() )
  188. urls.append( KUrl::fromPath( path ) );
  189.  
  190. m_collection->scanManager()->requestScan( urls, GenericScanManager::FullScan );
  191. }
  192.  
  193. void
  194. DatabaseCollectionScanCapability::startIncrementalScan( const QString &directory )
  195. {
  196. if( directory.isEmpty() )
  197. {
  198. QList<KUrl> urls;
  199. foreach( const QString& path, m_collection->mountPointManager()->collectionFolders() )
  200. urls.append( KUrl::fromPath( path ) );
  201.  
  202. m_collection->scanManager()->requestScan( urls, GenericScanManager::UpdateScan );
  203. }
  204. else
  205. {
  206. QList<KUrl> urls;
  207. urls.append( KUrl::fromPath( directory ) );
  208.  
  209. m_collection->scanManager()->requestScan( urls,
  210. GenericScanManager::PartialUpdateScan );
  211. }
  212. }
  213.  
  214. void
  215. DatabaseCollectionScanCapability::stopScan()
  216. {
  217. m_collection->scanManager()->abort();
  218. }
  219.  
  220. // --------- DatabaseCollectionImportCapability -------------
  221.  
  222. DatabaseCollectionImportCapability::DatabaseCollectionImportCapability( DatabaseCollection* collection )
  223. : m_collection( collection )
  224. {
  225. Q_ASSERT( m_collection );
  226. }
  227.  
  228. DatabaseCollectionImportCapability::~DatabaseCollectionImportCapability()
  229. { }
  230.  
  231. void
  232. DatabaseCollectionImportCapability::import( QIODevice *input, QObject *listener )
  233. {
  234. DEBUG_BLOCK
  235.  
  236. if( listener )
  237. {
  238. // TODO: change import capability to collection action
  239. // TODO: why have listeners here and not for the scan capability
  240. // TODO: showMessage does not longer work like this, the scan result processor is doing this
  241. connect( m_collection->scanManager(), SIGNAL(succeeded()),
  242. listener, SIGNAL(importSucceeded()) );
  243. connect( m_collection->scanManager(), SIGNAL(failed(QString)),
  244. listener, SIGNAL(showMessage(QString)) );
  245. }
  246.  
  247. m_collection->scanManager()->requestImport( input );
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement