1
/***************************************************************************
2
smb4ksharesview_part - This Part includes the shares icon view
6
copyright : (C) 2006 by Alexander Reinholdt
7
email : dustpuppy@users.berlios.de
8
***************************************************************************/
10
/***************************************************************************
11
* This program is free software; you can redistribute it and/or modify *
12
* it under the terms of the GNU General Public License as published by *
13
* the Free Software Foundation; either version 2 of the License, or *
14
* (at your option) any later version. *
16
* This program is distributed in the hope that it will be useful, but *
17
* WITHOUT ANY WARRANTY; without even the implied warranty of *
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
19
* General Public License for more details. *
21
* You should have received a copy of the GNU General Public License *
22
* along with this program; if not, write to the *
23
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
25
***************************************************************************/
29
#include <kstdaction.h>
31
#include <kaboutdata.h>
34
#include <kshortcut.h>
36
#include <kpopupmenu.h>
37
#include <kiconloader.h>
38
#include <kactionclasses.h>
40
// application specific includes
41
#include "smb4ksharesiconview_part.h"
42
#include "smb4ksharesiconview.h"
43
#include "smb4ksharesiconviewitem.h"
44
#include "../dialogs/smb4ksynchronizationdialog.h"
45
#include "../core/smb4kshare.h"
46
#include "../core/smb4kcore.h"
47
#include "../core/smb4ksettings.h"
50
KInstance *Smb4KSharesIconViewPartFactory::m_instance = 0L;
51
KAboutData *Smb4KSharesIconViewPartFactory::m_about = 0L;
54
Smb4KSharesIconViewPart::Smb4KSharesIconViewPart( QWidget *parentWidget, const char *widgetName,
55
QObject *parent, const char *name )
56
: KParts::Part( parent, name )
58
// First of all We need an instance:
59
setInstance( Smb4KSharesIconViewPartFactory::instance() );
62
setXMLFile( "smb4ksharesiconview_part.rc" );
64
// Set the widget of this part:
65
m_widget = new Smb4KSharesIconView( parentWidget, widgetName );
66
setWidget( m_widget );
68
// Set up the actions.
69
// Do not put this before setWidget() or the shortcuts
79
// Add some connections:
80
connect( Smb4KCore::mounter(), SIGNAL( updated() ),
81
this, SLOT( slotMountedShares() ) );
83
connect( Smb4KCore::synchronizer(), SIGNAL( state( int ) ),
84
this, SLOT( slotSynchronizationState( int ) ) );
86
connect( m_widget, SIGNAL( contextMenuRequested( QIconViewItem *, const QPoint & ) ),
87
this, SLOT( slotContextMenuRequested( QIconViewItem *, const QPoint & ) ) );
89
connect( m_widget, SIGNAL( selectionChanged( QIconViewItem * ) ),
90
this, SLOT( slotSelectionChanged( QIconViewItem * ) ) );
92
connect( m_widget, SIGNAL( pressed( QIconViewItem * ) ),
93
this, SLOT( slotMouseButtonPressed( QIconViewItem * ) ) );
95
connect( m_widget, SIGNAL( executed( QIconViewItem * ) ),
96
this, SLOT( slotFilemanager() ) );
100
Smb4KSharesIconViewPart::~Smb4KSharesIconViewPart()
105
void Smb4KSharesIconViewPart::setupActions()
107
// Create the actions:
108
KAction *unmount = new KAction( i18n( "&Unmount" ), "hdd_unmount", KShortcut( CTRL+Key_U ),
109
this, SLOT( slotUnmountShare() ),
110
actionCollection(), "unmount_action" );
112
KAction *force = new KAction( i18n( "&Force Unmounting" ), "hdd_unmount", KShortcut( CTRL+Key_F ),
113
this, SLOT( slotForceUnmountShare() ),
114
actionCollection(), "force_unmount_action" );
116
KAction *all = new KAction( i18n( "U&nmount All" ), "gear", KShortcut( CTRL+Key_N ),
117
this, SLOT( slotUnmountAllShares() ),
118
actionCollection(), "unmount_all_action" );
119
KAction *sync = new KAction( i18n( "S&ynchronize" ), "bottom", KShortcut( CTRL+Key_Y ),
120
this, SLOT( slotSynchronize() ),
121
actionCollection(), "synchronize_action" );
122
KAction *konsole = new KAction( i18n( "Open with Konso&le" ), "terminal", KShortcut( CTRL+Key_L ),
123
this, SLOT( slotKonsole() ),
124
actionCollection(), "konsole_action" );
125
KAction *konq = new KAction( i18n( "Open with &Konqueror" ), "kfm_home", KShortcut( CTRL+Key_K ),
126
this, SLOT( slotFilemanager() ),
127
actionCollection(), "filemanager_action" );
129
// Disable all actions for now:
130
unmount->setEnabled( false );
132
force->setEnabled( false );
134
all->setEnabled( false );
135
sync->setEnabled( false );
136
konsole->setEnabled( false );
137
konq->setEnabled( false );
139
// Insert the actions into the menu:
140
m_menu = new KActionMenu( this, "SharesIconViewMenu" );
141
m_menu->popupMenu()->insertTitle( SmallIcon( "hdd_mount" ), i18n( "Shares" ), 0 );
142
m_menu->insert( unmount, -1 );
144
m_menu->insert( force, -1 );
146
m_menu->insert( all, -1 );
147
m_menu->popupMenu()->insertSeparator( -1 );
148
m_menu->insert( sync, -1 );
149
m_menu->popupMenu()->insertSeparator( -1 );
150
m_menu->insert( konsole, -1 );
151
m_menu->insert( konq, -1 );
155
void Smb4KSharesIconViewPart::loadSettings()
158
actionCollection()->action( "force_unmount_action" )->setEnabled( Smb4KSettings::useForceUnmount() );
161
// Change the text of the share:
162
for ( QIconViewItem *it = m_widget->firstItem(); it; it = it->nextItem() )
164
Smb4KSharesIconViewItem *item = static_cast<Smb4KSharesIconViewItem *>( it );
168
item->setText( (Smb4KSettings::showMountPoint() ?
169
item->shareObject()->path() :
170
item->shareObject()->name()) );
174
// Enable/disable support for dropping:
175
m_widget->setAcceptDrops( Smb4KSettings::enableDropSupport() );
177
// Load or remove all foreign shares. The easiest way to do this
178
// is to invoke slotMountedShares():
183
void Smb4KSharesIconViewPart::customEvent( QCustomEvent *e )
187
case EVENT_LOAD_SETTINGS:
194
case EVENT_SET_FOCUS:
196
KIconView *view = static_cast<KIconView *>( m_widget );
198
if ( view->count() != 0 )
200
view->setSelected( !view->currentItem() ?
202
view->currentItem(), true, false );
215
KParts::Part::customEvent( e );
219
/////////////////////////////////////////////////////////////////////////////
220
// SLOT IMPLEMENTATIONS (Smb4KSharesIconViewPart)
221
/////////////////////////////////////////////////////////////////////////////
223
void Smb4KSharesIconViewPart::slotContextMenuRequested( QIconViewItem *item, const QPoint &pos )
227
m_menu->popupMenu()->changeTitle( 0, SmallIcon( "hdd_mount" ),
228
static_cast<Smb4KSharesIconViewItem *>( item )->shareObject()->name() );
232
m_menu->popupMenu()->changeTitle( 0, SmallIcon( "hdd_mount" ), i18n( "Shares" ) );
235
m_menu->popupMenu()->exec( pos, 0 );
239
void Smb4KSharesIconViewPart::slotSelectionChanged( QIconViewItem *item )
241
// NOTE: Here we only enable or disable the KActions. All other things
242
// are done in the Smb4KSharesIconView class.
244
// This slot is used to enable or disable the actions when the user
245
// changes the item in the icon view. This slot won't be called when
246
// the user clicks on the view port!
249
actionCollection()->action( "unmount_action" )->setEnabled( true );
251
actionCollection()->action( "force_unmount_action" )->setEnabled( Smb4KSettings::useForceUnmount() );
253
actionCollection()->action( "unmount_all_action" )->setEnabled( true );
255
Smb4KShare *share = static_cast<Smb4KSharesIconViewItem *>( item )->shareObject();
257
if ( !share->isBroken() )
259
actionCollection()->action( "konsole_action" )->setEnabled( !Smb4KSettings::konsole().isEmpty() );
260
actionCollection()->action( "filemanager_action" )->setEnabled( true );
261
actionCollection()->action( "synchronize_action" )->setEnabled( !Smb4KSettings::rsync().isEmpty() &&
262
!Smb4KCore::synchronizer()->isRunning() );
266
actionCollection()->action( "konsole_action" )->setEnabled( false );
267
actionCollection()->action( "filemanager_action" )->setEnabled( false );
268
actionCollection()->action( "synchronize_action" )->setEnabled( false );
273
// Smb4KSharesIconViewPart::slotMouseButtonPressed()
278
void Smb4KSharesIconViewPart::slotMouseButtonPressed( QIconViewItem *item )
280
// NOTE: Here we only enable or disable the KActions. All other things
281
// are done in the Smb4KSharesIconView class.
283
// Here we do all the stuff that cannot be done in
284
// Smb4KSharesIconViewPart::slotSelectionChanged()
287
actionCollection()->action( "unmount_action" )->setEnabled( false );
289
actionCollection()->action( "force_unmount_action" )->setEnabled( false );
291
actionCollection()->action( "unmount_all_action" )->setEnabled( (m_widget->count() > 0) );
292
actionCollection()->action( "konsole_action" )->setEnabled( false );
293
actionCollection()->action( "filemanager_action" )->setEnabled( false );
294
actionCollection()->action( "synchronize_action" )->setEnabled( false );
298
// See Smb4KSharesIconViewPart::slotSelectionChanged()
303
void Smb4KSharesIconViewPart::slotMountedShares()
305
// Get the list of shares:
306
QValueList<Smb4KShare *> list = Smb4KCore::mounter()->getShares();
309
if ( !list.isEmpty() )
311
// Remove all obsolete items:
312
Smb4KSharesIconViewItem *test_item = static_cast<Smb4KSharesIconViewItem *>( m_widget->firstItem() );
313
Smb4KSharesIconViewItem *next_item = NULL;
317
Smb4KShare *share = Smb4KCore::mounter()->findShareByPath( test_item->shareObject()->path() );
318
next_item = static_cast<Smb4KSharesIconViewItem *>( test_item->nextItem() );
320
if( !share || (test_item->shareObject()->isForeign() && !Smb4KSettings::showAllShares()) )
326
test_item = next_item;
329
// Now process the entries in the list:
330
bool already_in_view = false;
332
for ( QValueListConstIterator<Smb4KShare *> it = list.begin(); it != list.end(); ++it )
334
// Check, whether the share is already in the list. Look for the
336
for ( Smb4KSharesIconViewItem *item = static_cast<Smb4KSharesIconViewItem *>( m_widget->firstItem() );
337
item; item = static_cast<Smb4KSharesIconViewItem *>( item->nextItem() ) )
339
if ( QString::compare( item->shareObject()->path(), (*it)->path() ) == 0 ||
340
QString::compare( item->shareObject()->canonicalPath(), (*it)->canonicalPath() ) == 0 )
342
// Replace the share object if something changed:
343
if ( !item->sameShareObject( *it ) )
345
item->replaceShareObject( *it );
348
already_in_view = true;
358
if ( !already_in_view )
360
if ( !Smb4KSettings::showAllShares() && (*it)->isForeign() )
362
// If the user does not want to have foreign shares
363
// displayed continue.
368
// Put the new item into the icon view:
369
(void) new Smb4KSharesIconViewItem( *it, Smb4KSettings::showMountPoint(), m_widget );
375
already_in_view = false;
378
m_widget->sort( m_widget->sortDirection() );
385
// Update the tool tip, if it exists:
386
if ( m_widget->count() != 0 )
388
m_widget->updateToolTip();
391
// Enable/disable the actions:
392
QIconViewItem *item = m_widget->currentItem();
393
bool have_selected_item = (item && item->isSelected());
395
actionCollection()->action( "unmount_action" )->setEnabled( have_selected_item );
397
if ( Smb4KSettings::useForceUnmount() )
399
actionCollection()->action( "force_unmount_action" )->setEnabled( have_selected_item );
403
actionCollection()->action( "force_unmount_action" )->setEnabled( false );
406
actionCollection()->action( "unmount_all_action" )->setEnabled( (m_widget->count() > 0) );
407
actionCollection()->action( "konsole_action" )->setEnabled( !Smb4KSettings::konsole().isEmpty() &&
408
have_selected_item );
409
actionCollection()->action( "filemanager_action" )->setEnabled( have_selected_item );
410
actionCollection()->action( "synchronize_action" )->setEnabled( !Smb4KSettings::rsync().isEmpty() &&
411
!Smb4KCore::synchronizer()->isRunning() &&
412
have_selected_item );
416
void Smb4KSharesIconViewPart::slotUnmountShare()
418
Smb4KSharesIconViewItem *item = static_cast<Smb4KSharesIconViewItem *>( m_widget->currentItem() );
422
Smb4KCore::mounter()->unmountShare( item->shareObject(), false );
427
void Smb4KSharesIconViewPart::slotForceUnmountShare()
429
Smb4KSharesIconViewItem *item = static_cast<Smb4KSharesIconViewItem *>( m_widget->currentItem() );
433
Smb4KCore::mounter()->unmountShare( item->shareObject(), true );
438
void Smb4KSharesIconViewPart::slotUnmountAllShares()
440
Smb4KCore::mounter()->unmountAllShares();
444
void Smb4KSharesIconViewPart::slotSynchronize()
446
Smb4KSharesIconViewItem *item = static_cast<Smb4KSharesIconViewItem *>( m_widget->currentItem() );
447
Smb4KSynchronizationDialog *dlg = static_cast<Smb4KSynchronizationDialog *>( m_widget->child( "SynchronizationDialog", "Smb4KSynchronizationDialog", true ) );
449
if ( item && !item->shareObject()->isBroken() && !dlg )
451
dlg = new Smb4KSynchronizationDialog( item->shareObject(), m_widget, "SynchronizationDialog" );
457
void Smb4KSharesIconViewPart::slotKonsole()
459
Smb4KSharesIconViewItem *item = static_cast<Smb4KSharesIconViewItem *>( m_widget->currentItem() );
461
if ( item && !item->shareObject()->isBroken() )
463
Smb4KCore::open( item->shareObject(), Smb4KCore::Konsole );
468
void Smb4KSharesIconViewPart::slotFilemanager()
470
Smb4KSharesIconViewItem *item = static_cast<Smb4KSharesIconViewItem *>( m_widget->currentItem() );
474
// Workaround: The item is not selected if you execute it, so
475
// we have to do it here:
476
// item->setSelected( true );
478
if ( !item->shareObject()->isBroken() )
480
Smb4KCore::open( item->shareObject(), Smb4KCore::Konqueror );
486
void Smb4KSharesIconViewPart::slotSynchronizationState( int state )
490
case SYNCHRONIZER_START:
492
actionCollection()->action( "synchronize_action" )->setEnabled( false );
496
case SYNCHRONIZER_STOP:
498
actionCollection()->action( "synchronize_action" )->setEnabled( true );
511
/////////////////////////////////////////////////////////////////////////////
513
/////////////////////////////////////////////////////////////////////////////
515
Smb4KSharesIconViewPartFactory::Smb4KSharesIconViewPartFactory()
521
Smb4KSharesIconViewPartFactory::~Smb4KSharesIconViewPartFactory()
530
KParts::Part *Smb4KSharesIconViewPartFactory::createPartObject( QWidget *parentWidget, const char *widgetName,
531
QObject *parent, const char *name, const char *, const QStringList & )
533
Smb4KSharesIconViewPart *obj = new Smb4KSharesIconViewPart( parentWidget, widgetName, parent, name );
535
// See if we are to be read-write or not
536
// if (QCString(classname) == "KParts::ReadOnlyPart")
538
// obj->setReadWrite(false);
545
KInstance *Smb4KSharesIconViewPartFactory::instance()
549
m_about = new KAboutData( "smb4ksharesiconviewpart", I18N_NOOP( "Smb4KSharesIconViewPart" ), "1.0" );
550
m_about->addAuthor("Alexander Reinholdt", 0, "dustpuppy@users.berlios.de");
551
m_about->setLicense( KAboutData::License_GPL );
552
m_instance = new KInstance( m_about );
559
/////////////////////////////////////////////////////////////////////////////
561
/////////////////////////////////////////////////////////////////////////////
565
void *init_libsmb4ksharesiconview()
567
KGlobal::locale()->insertCatalogue( "smb4k" );
568
return new Smb4KSharesIconViewPartFactory;
573
#include "smb4ksharesiconview_part.moc"