Smb4K – Samba (SMB) share advanced browser
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

574 lines
18KB

  1. /***************************************************************************
  2. smb4ksharesview_part - This Part includes the shares icon view
  3. of Smb4K.
  4. -------------------
  5. begin : Mo Dez 4 2006
  6. copyright : (C) 2006 by Alexander Reinholdt
  7. email : dustpuppy@users.berlios.de
  8. ***************************************************************************/
  9. /***************************************************************************
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. * This program is distributed in the hope that it will be useful, but *
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of *
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
  18. * General Public License for more details. *
  19. * *
  20. * You should have received a copy of the GNU General Public License *
  21. * along with this program; if not, write to the *
  22. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, *
  23. * MA 02110-1301 USA *
  24. ***************************************************************************/
  25. // KDE includes
  26. #include <kglobal.h>
  27. #include <kstdaction.h>
  28. #include <klocale.h>
  29. #include <kaboutdata.h>
  30. #include <klocale.h>
  31. #include <kaction.h>
  32. #include <kshortcut.h>
  33. #include <kdebug.h>
  34. #include <kpopupmenu.h>
  35. #include <kiconloader.h>
  36. #include <kactionclasses.h>
  37. // application specific includes
  38. #include "smb4ksharesiconview_part.h"
  39. #include "smb4ksharesiconview.h"
  40. #include "smb4ksharesiconviewitem.h"
  41. #include "../dialogs/smb4ksynchronizationdialog.h"
  42. #include "../core/smb4kshare.h"
  43. #include "../core/smb4kcore.h"
  44. #include "../core/smb4ksettings.h"
  45. KInstance *Smb4KSharesIconViewPartFactory::m_instance = 0L;
  46. KAboutData *Smb4KSharesIconViewPartFactory::m_about = 0L;
  47. Smb4KSharesIconViewPart::Smb4KSharesIconViewPart( TQWidget *parentWidget, const char *widgetName,
  48. TQObject *parent, const char *name )
  49. : KParts::Part( parent, name )
  50. {
  51. // First of all We need an instance:
  52. setInstance( Smb4KSharesIconViewPartFactory::instance() );
  53. // Set the XML file:
  54. setXMLFile( "smb4ksharesiconview_part.rc" );
  55. // Set the widget of this part:
  56. m_widget = new Smb4KSharesIconView( parentWidget, widgetName );
  57. setWidget( m_widget );
  58. // Set up the actions.
  59. // Do not put this before setWidget() or the shortcuts
  60. // will not be shown.
  61. setupActions();
  62. // Import the shares:
  63. slotMountedShares();
  64. // Load settings:
  65. loadSettings();
  66. // Add some connections:
  67. connect( Smb4KCore::mounter(), TQT_SIGNAL( updated() ),
  68. this, TQT_SLOT( slotMountedShares() ) );
  69. connect( Smb4KCore::synchronizer(), TQT_SIGNAL( state( int ) ),
  70. this, TQT_SLOT( slotSynchronizationState( int ) ) );
  71. connect( m_widget, TQT_SIGNAL( contextMenuRequested( TQIconViewItem *, const TQPoint & ) ),
  72. this, TQT_SLOT( slotContextMenuRequested( TQIconViewItem *, const TQPoint & ) ) );
  73. connect( m_widget, TQT_SIGNAL( selectionChanged( TQIconViewItem * ) ),
  74. this, TQT_SLOT( slotSelectionChanged( TQIconViewItem * ) ) );
  75. connect( m_widget, TQT_SIGNAL( pressed( TQIconViewItem * ) ),
  76. this, TQT_SLOT( slotMouseButtonPressed( TQIconViewItem * ) ) );
  77. connect( m_widget, TQT_SIGNAL( executed( TQIconViewItem * ) ),
  78. this, TQT_SLOT( slotFilemanager() ) );
  79. }
  80. Smb4KSharesIconViewPart::~Smb4KSharesIconViewPart()
  81. {
  82. }
  83. void Smb4KSharesIconViewPart::setupActions()
  84. {
  85. // Create the actions:
  86. KAction *unmount = new KAction( i18n( "&Unmount" ), "hdd_unmount", KShortcut( CTRL+Key_U ),
  87. this, TQT_SLOT( slotUnmountShare() ),
  88. actionCollection(), "unmount_action" );
  89. #ifdef __linux__
  90. KAction *force = new KAction( i18n( "&Force Unmounting" ), "hdd_unmount", KShortcut( CTRL+Key_F ),
  91. this, TQT_SLOT( slotForceUnmountShare() ),
  92. actionCollection(), "force_unmount_action" );
  93. #endif
  94. KAction *all = new KAction( i18n( "U&nmount All" ), "gear", KShortcut( CTRL+Key_N ),
  95. this, TQT_SLOT( slotUnmountAllShares() ),
  96. actionCollection(), "unmount_all_action" );
  97. KAction *sync = new KAction( i18n( "S&ynchronize" ), "bottom", KShortcut( CTRL+Key_Y ),
  98. this, TQT_SLOT( slotSynchronize() ),
  99. actionCollection(), "synchronize_action" );
  100. KAction *konsole = new KAction( i18n( "Open with Konso&le" ), "terminal", KShortcut( CTRL+Key_L ),
  101. this, TQT_SLOT( slotKonsole() ),
  102. actionCollection(), "konsole_action" );
  103. KAction *konq = new KAction( i18n( "Open with &Konqueror" ), "kfm_home", KShortcut( CTRL+Key_K ),
  104. this, TQT_SLOT( slotFilemanager() ),
  105. actionCollection(), "filemanager_action" );
  106. // Disable all actions for now:
  107. unmount->setEnabled( false );
  108. #ifdef __linux__
  109. force->setEnabled( false );
  110. #endif
  111. all->setEnabled( false );
  112. sync->setEnabled( false );
  113. konsole->setEnabled( false );
  114. konq->setEnabled( false );
  115. // Insert the actions into the menu:
  116. m_menu = new KActionMenu( this, "SharesIconViewMenu" );
  117. m_menu->popupMenu()->insertTitle( SmallIcon( "hdd_mount" ), i18n( "Shares" ), 0 );
  118. m_menu->insert( unmount, -1 );
  119. #ifdef __linux__
  120. m_menu->insert( force, -1 );
  121. #endif
  122. m_menu->insert( all, -1 );
  123. m_menu->popupMenu()->insertSeparator( -1 );
  124. m_menu->insert( sync, -1 );
  125. m_menu->popupMenu()->insertSeparator( -1 );
  126. m_menu->insert( konsole, -1 );
  127. m_menu->insert( konq, -1 );
  128. }
  129. void Smb4KSharesIconViewPart::loadSettings()
  130. {
  131. #ifdef __linux__
  132. actionCollection()->action( "force_unmount_action" )->setEnabled( Smb4KSettings::useForceUnmount() );
  133. #endif
  134. // Change the text of the share:
  135. for ( TQIconViewItem *it = m_widget->firstItem(); it; it = it->nextItem() )
  136. {
  137. Smb4KSharesIconViewItem *item = static_cast<Smb4KSharesIconViewItem *>( it );
  138. if ( item )
  139. {
  140. item->setText( (Smb4KSettings::showMountPoint() ?
  141. item->shareObject()->path() :
  142. item->shareObject()->name()) );
  143. }
  144. }
  145. // Enable/disable support for dropping:
  146. m_widget->setAcceptDrops( Smb4KSettings::enableDropSupport() );
  147. // Load or remove all foreign shares. The easiest way to do this
  148. // is to invoke slotMountedShares():
  149. slotMountedShares();
  150. }
  151. void Smb4KSharesIconViewPart::customEvent( TQCustomEvent *e )
  152. {
  153. switch ( e->type() )
  154. {
  155. case EVENT_LOAD_SETTINGS:
  156. {
  157. loadSettings();
  158. slotMountedShares();
  159. break;
  160. }
  161. case EVENT_SET_FOCUS:
  162. {
  163. KIconView *view = static_cast<KIconView *>( m_widget );
  164. if ( view->count() != 0 )
  165. {
  166. view->setSelected( !view->currentItem() ?
  167. view->firstItem() :
  168. view->currentItem(), true, false );
  169. }
  170. view->setFocus();
  171. break;
  172. }
  173. default:
  174. {
  175. break;
  176. }
  177. }
  178. KParts::Part::customEvent( e );
  179. }
  180. /////////////////////////////////////////////////////////////////////////////
  181. // TQT_SLOT IMPLEMENTATIONS (Smb4KSharesIconViewPart)
  182. /////////////////////////////////////////////////////////////////////////////
  183. void Smb4KSharesIconViewPart::slotContextMenuRequested( TQIconViewItem *item, const TQPoint &pos )
  184. {
  185. if ( item )
  186. {
  187. m_menu->popupMenu()->changeTitle( 0, SmallIcon( "hdd_mount" ),
  188. static_cast<Smb4KSharesIconViewItem *>( item )->shareObject()->name() );
  189. }
  190. else
  191. {
  192. m_menu->popupMenu()->changeTitle( 0, SmallIcon( "hdd_mount" ), i18n( "Shares" ) );
  193. }
  194. m_menu->popupMenu()->exec( pos, 0 );
  195. }
  196. void Smb4KSharesIconViewPart::slotSelectionChanged( TQIconViewItem *item )
  197. {
  198. // NOTE: Here we only enable or disable the KActions. All other things
  199. // are done in the Smb4KSharesIconView class.
  200. // This slot is used to enable or disable the actions when the user
  201. // changes the item in the icon view. This slot won't be called when
  202. // the user clicks on the view port!
  203. if ( item )
  204. {
  205. actionCollection()->action( "unmount_action" )->setEnabled( true );
  206. #ifdef __linux__
  207. actionCollection()->action( "force_unmount_action" )->setEnabled( Smb4KSettings::useForceUnmount() );
  208. #endif
  209. actionCollection()->action( "unmount_all_action" )->setEnabled( true );
  210. Smb4KShare *share = static_cast<Smb4KSharesIconViewItem *>( item )->shareObject();
  211. if ( !share->isBroken() )
  212. {
  213. actionCollection()->action( "konsole_action" )->setEnabled( !Smb4KSettings::konsole().isEmpty() );
  214. actionCollection()->action( "filemanager_action" )->setEnabled( true );
  215. actionCollection()->action( "synchronize_action" )->setEnabled( !Smb4KSettings::rsync().isEmpty() &&
  216. !Smb4KCore::synchronizer()->isRunning() );
  217. }
  218. else
  219. {
  220. actionCollection()->action( "konsole_action" )->setEnabled( false );
  221. actionCollection()->action( "filemanager_action" )->setEnabled( false );
  222. actionCollection()->action( "synchronize_action" )->setEnabled( false );
  223. }
  224. }
  225. else
  226. {
  227. // Smb4KSharesIconViewPart::slotMouseButtonPressed()
  228. }
  229. }
  230. void Smb4KSharesIconViewPart::slotMouseButtonPressed( TQIconViewItem *item )
  231. {
  232. // NOTE: Here we only enable or disable the KActions. All other things
  233. // are done in the Smb4KSharesIconView class.
  234. // Here we do all the stuff that cannot be done in
  235. // Smb4KSharesIconViewPart::slotSelectionChanged()
  236. if ( !item )
  237. {
  238. actionCollection()->action( "unmount_action" )->setEnabled( false );
  239. #ifdef __linux__
  240. actionCollection()->action( "force_unmount_action" )->setEnabled( false );
  241. #endif
  242. actionCollection()->action( "unmount_all_action" )->setEnabled( (m_widget->count() > 0) );
  243. actionCollection()->action( "konsole_action" )->setEnabled( false );
  244. actionCollection()->action( "filemanager_action" )->setEnabled( false );
  245. actionCollection()->action( "synchronize_action" )->setEnabled( false );
  246. }
  247. else
  248. {
  249. // See Smb4KSharesIconViewPart::slotSelectionChanged()
  250. }
  251. }
  252. void Smb4KSharesIconViewPart::slotMountedShares()
  253. {
  254. // Get the list of shares:
  255. TQValueList<Smb4KShare *> list = Smb4KCore::mounter()->getShares();
  256. // Update the view:
  257. if ( !list.isEmpty() )
  258. {
  259. // Remove all obsolete items:
  260. Smb4KSharesIconViewItem *test_item = static_cast<Smb4KSharesIconViewItem *>( m_widget->firstItem() );
  261. Smb4KSharesIconViewItem *next_item = NULL;
  262. while ( test_item )
  263. {
  264. Smb4KShare *share = Smb4KCore::mounter()->findShareByPath( test_item->shareObject()->path() );
  265. next_item = static_cast<Smb4KSharesIconViewItem *>( test_item->nextItem() );
  266. if( !share || (test_item->shareObject()->isForeign() && !Smb4KSettings::showAllShares()) )
  267. {
  268. delete test_item;
  269. test_item = NULL;
  270. }
  271. test_item = next_item;
  272. }
  273. // Now process the entries in the list:
  274. bool already_in_view = false;
  275. for ( TQValueListConstIterator<Smb4KShare *> it = list.begin(); it != list.end(); ++it )
  276. {
  277. // Check, whether the share is already in the list. Look for the
  278. // mount point.
  279. for ( Smb4KSharesIconViewItem *item = static_cast<Smb4KSharesIconViewItem *>( m_widget->firstItem() );
  280. item; item = static_cast<Smb4KSharesIconViewItem *>( item->nextItem() ) )
  281. {
  282. if ( TQString::compare( item->shareObject()->path(), (*it)->path() ) == 0 ||
  283. TQString::compare( item->shareObject()->canonicalPath(), (*it)->canonicalPath() ) == 0 )
  284. {
  285. // Replace the share object if something changed:
  286. if ( !item->sameShareObject( *it ) )
  287. {
  288. item->replaceShareObject( *it );
  289. }
  290. already_in_view = true;
  291. break;
  292. }
  293. else
  294. {
  295. continue;
  296. }
  297. }
  298. if ( !already_in_view )
  299. {
  300. if ( !Smb4KSettings::showAllShares() && (*it)->isForeign() )
  301. {
  302. // If the user does not want to have foreign shares
  303. // displayed continue.
  304. continue;
  305. }
  306. else
  307. {
  308. // Put the new item into the icon view:
  309. (void) new Smb4KSharesIconViewItem( *it, Smb4KSettings::showMountPoint(), m_widget );
  310. continue;
  311. }
  312. }
  313. already_in_view = false;
  314. }
  315. m_widget->sort( m_widget->sortDirection() );
  316. }
  317. else
  318. {
  319. m_widget->clear();
  320. }
  321. // Update the tool tip, if it exists:
  322. if ( m_widget->count() != 0 )
  323. {
  324. m_widget->updateToolTip();
  325. }
  326. // Enable/disable the actions:
  327. TQIconViewItem *item = m_widget->currentItem();
  328. bool have_selected_item = (item && item->isSelected());
  329. actionCollection()->action( "unmount_action" )->setEnabled( have_selected_item );
  330. #ifdef __linux__
  331. if ( Smb4KSettings::useForceUnmount() )
  332. {
  333. actionCollection()->action( "force_unmount_action" )->setEnabled( have_selected_item );
  334. }
  335. else
  336. {
  337. actionCollection()->action( "force_unmount_action" )->setEnabled( false );
  338. }
  339. #endif
  340. actionCollection()->action( "unmount_all_action" )->setEnabled( (m_widget->count() > 0) );
  341. actionCollection()->action( "konsole_action" )->setEnabled( !Smb4KSettings::konsole().isEmpty() &&
  342. have_selected_item );
  343. actionCollection()->action( "filemanager_action" )->setEnabled( have_selected_item );
  344. actionCollection()->action( "synchronize_action" )->setEnabled( !Smb4KSettings::rsync().isEmpty() &&
  345. !Smb4KCore::synchronizer()->isRunning() &&
  346. have_selected_item );
  347. }
  348. void Smb4KSharesIconViewPart::slotUnmountShare()
  349. {
  350. Smb4KSharesIconViewItem *item = static_cast<Smb4KSharesIconViewItem *>( m_widget->currentItem() );
  351. if ( item )
  352. {
  353. Smb4KCore::mounter()->unmountShare( item->shareObject(), false );
  354. }
  355. }
  356. void Smb4KSharesIconViewPart::slotForceUnmountShare()
  357. {
  358. Smb4KSharesIconViewItem *item = static_cast<Smb4KSharesIconViewItem *>( m_widget->currentItem() );
  359. if ( item )
  360. {
  361. Smb4KCore::mounter()->unmountShare( item->shareObject(), true );
  362. }
  363. }
  364. void Smb4KSharesIconViewPart::slotUnmountAllShares()
  365. {
  366. Smb4KCore::mounter()->unmountAllShares();
  367. }
  368. void Smb4KSharesIconViewPart::slotSynchronize()
  369. {
  370. Smb4KSharesIconViewItem *item = static_cast<Smb4KSharesIconViewItem *>( m_widget->currentItem() );
  371. Smb4KSynchronizationDialog *dlg = static_cast<Smb4KSynchronizationDialog *>( TQT_TQWIDGET(m_widget->child( "SynchronizationDialog", "Smb4KSynchronizationDialog", true )) );
  372. if ( item && !item->shareObject()->isBroken() && !dlg )
  373. {
  374. dlg = new Smb4KSynchronizationDialog( item->shareObject(), m_widget, "SynchronizationDialog" );
  375. dlg->show();
  376. }
  377. }
  378. void Smb4KSharesIconViewPart::slotKonsole()
  379. {
  380. Smb4KSharesIconViewItem *item = static_cast<Smb4KSharesIconViewItem *>( m_widget->currentItem() );
  381. if ( item && !item->shareObject()->isBroken() )
  382. {
  383. Smb4KCore::open( item->shareObject(), Smb4KCore::Konsole );
  384. }
  385. }
  386. void Smb4KSharesIconViewPart::slotFilemanager()
  387. {
  388. Smb4KSharesIconViewItem *item = static_cast<Smb4KSharesIconViewItem *>( m_widget->currentItem() );
  389. if ( item )
  390. {
  391. // Workaround: The item is not selected if you execute it, so
  392. // we have to do it here:
  393. // item->setSelected( true );
  394. if ( !item->shareObject()->isBroken() )
  395. {
  396. Smb4KCore::open( item->shareObject(), Smb4KCore::Konqueror );
  397. }
  398. }
  399. }
  400. void Smb4KSharesIconViewPart::slotSynchronizationState( int state )
  401. {
  402. switch ( state )
  403. {
  404. case SYNCHRONIZER_START:
  405. {
  406. actionCollection()->action( "synchronize_action" )->setEnabled( false );
  407. break;
  408. }
  409. case SYNCHRONIZER_STOP:
  410. {
  411. actionCollection()->action( "synchronize_action" )->setEnabled( true );
  412. break;
  413. }
  414. default:
  415. {
  416. break;
  417. }
  418. }
  419. }
  420. /////////////////////////////////////////////////////////////////////////////
  421. // FACTORY STUFF
  422. /////////////////////////////////////////////////////////////////////////////
  423. Smb4KSharesIconViewPartFactory::Smb4KSharesIconViewPartFactory()
  424. : KParts::Factory()
  425. {
  426. }
  427. Smb4KSharesIconViewPartFactory::~Smb4KSharesIconViewPartFactory()
  428. {
  429. delete m_instance;
  430. delete m_about;
  431. m_instance = 0L;
  432. }
  433. KParts::Part *Smb4KSharesIconViewPartFactory::createPartObject( TQWidget *parentWidget, const char *widgetName,
  434. TQObject *parent, const char *name, const char *, const TQStringList & )
  435. {
  436. Smb4KSharesIconViewPart *obj = new Smb4KSharesIconViewPart( parentWidget, widgetName, parent, name );
  437. // See if we are to be read-write or not
  438. // if (TQCString(classname) == "KParts::ReadOnlyPart")
  439. // {
  440. // obj->setReadWrite(false);
  441. // }
  442. return obj;
  443. }
  444. KInstance *Smb4KSharesIconViewPartFactory::instance()
  445. {
  446. if( !m_instance )
  447. {
  448. m_about = new KAboutData( "smb4ksharesiconviewpart", I18N_NOOP( "Smb4KSharesIconViewPart" ), "1.0" );
  449. m_about->addAuthor("Alexander Reinholdt", 0, "dustpuppy@users.berlios.de");
  450. m_about->setLicense( KAboutData::License_GPL );
  451. m_instance = new KInstance( m_about );
  452. }
  453. return m_instance;
  454. }
  455. /////////////////////////////////////////////////////////////////////////////
  456. // INIT
  457. /////////////////////////////////////////////////////////////////////////////
  458. extern "C"
  459. {
  460. void *init_libsmb4ksharesiconview()
  461. {
  462. KGlobal::locale()->insertCatalogue( "smb4k" );
  463. return new Smb4KSharesIconViewPartFactory;
  464. }
  465. }
  466. #include "smb4ksharesiconview_part.moc"