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.
 
 
 
 
 
 

400 lines
11 KiB

  1. /***************************************************************************
  2. smb4kpreviewdialog.cpp - The preview dialog of Smb4K
  3. -------------------
  4. begin : Fre Jul 4 2003
  5. copyright : (C) 2003-2007 by Alexander Reinholdt
  6. email : dustpuppy@users.berlios.de
  7. ***************************************************************************/
  8. /***************************************************************************
  9. * This program is free software; you can redistribute it and/or modify *
  10. * it under the terms of the GNU General Public License as published by *
  11. * the Free Software Foundation; either version 2 of the License, or *
  12. * (at your option) any later version. *
  13. * *
  14. * This program is distributed in the hope that it will be useful, but *
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of *
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
  17. * General Public License for more details. *
  18. * *
  19. * You should have received a copy of the GNU General Public License *
  20. * along with this program; if not, write to the *
  21. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, *
  22. * MA 02110-1301 USA *
  23. ***************************************************************************/
  24. // TQt includes
  25. #include <tqlayout.h>
  26. #include <tqtooltip.h>
  27. #include <tqiconset.h>
  28. // KDE includes
  29. #include <tdelocale.h>
  30. #include <tdeapplication.h>
  31. #include <kiconloader.h>
  32. #include <kdebug.h>
  33. #include <tdetoolbarbutton.h>
  34. // application specific includes
  35. #include "smb4kpreviewdialog.h"
  36. #include "../core/smb4kcore.h"
  37. #include "../core/smb4knetworkitems.h"
  38. #include "../core/smb4ksettings.h"
  39. Smb4KPreviewDialog::Smb4KPreviewDialog( Smb4KShareItem *item, TQWidget *parent, const char *name )
  40. : KDialogBase( Plain, i18n( "Preview" ), Close, Close, parent, name, false, true )
  41. {
  42. setWFlags( TQt::WDestructiveClose );
  43. m_item = new Smb4KPreviewItem( item );
  44. if ( m_item )
  45. {
  46. if ( Smb4KHostItem *host = Smb4KCore::scanner()->getHost( item->host(), item->workgroup() ) )
  47. {
  48. m_item->setIP( host->ip() );
  49. }
  50. m_button_id = None;
  51. m_current_item = m_history.end();
  52. setupView();
  53. setInitialSize( configDialogSize( *(Smb4KSettings::self()->config()), "PreviewDialog" ) );
  54. connect( m_view, TQT_SIGNAL( executed( TQIconViewItem * ) ),
  55. this, TQT_SLOT( slotItemExecuted( TQIconViewItem * ) ) );
  56. connect( m_toolbar, TQT_SIGNAL( clicked( int ) ),
  57. this, TQT_SLOT( slotButtonClicked( int ) ) );
  58. connect( m_combo, TQT_SIGNAL( activated( const TQString & ) ),
  59. this, TQT_SLOT( slotItemActivated( const TQString & ) ) );
  60. connect( Smb4KCore::previewer(), TQT_SIGNAL( result( Smb4KPreviewItem * ) ),
  61. this, TQT_SLOT( slotReceivedData( Smb4KPreviewItem * ) ) );
  62. m_initialized = Smb4KCore::previewer()->preview( m_item );
  63. }
  64. setMinimumSize( (sizeHint().width() > 350 ? sizeHint().width() : 350), sizeHint().height() );
  65. }
  66. Smb4KPreviewDialog::~Smb4KPreviewDialog()
  67. {
  68. delete m_item;
  69. }
  70. void Smb4KPreviewDialog::setupView()
  71. {
  72. TQFrame *frame = plainPage();
  73. TQGridLayout *layout = new TQGridLayout( frame );
  74. m_view = new TDEIconView( frame, 0, 0 );
  75. m_view->setItemTextPos( TDEIconView::Right );
  76. m_view->setResizeMode( TDEIconView::Adjust );
  77. m_view->setArrangement( TDEIconView::TopToBottom );
  78. m_view->setSpacing( 1 );
  79. m_view->setGridX( 200 );
  80. m_view->setWordWrapIconText( false );
  81. m_view->setShowToolTips( true );
  82. m_view->setAutoArrange( true );
  83. m_view->setSorting( true, true );
  84. m_toolbar = new TDEToolBar( frame, 0, true, false );
  85. m_toolbar->insertButton( "reload", Reload, true, i18n( "Reload" ), 0 );
  86. m_toolbar->insertButton( "back", Back, false, i18n( "Back" ), 1 );
  87. m_toolbar->insertButton( "forward", Forward, false, i18n( "Forward" ), 2 );
  88. m_toolbar->insertButton( "go-up", Up, false, i18n( "Up" ), 3 );
  89. m_combo = new KComboBox( false, m_toolbar, 0 );
  90. m_combo->listBox()->setHScrollBarMode( TQScrollView::Auto );
  91. m_combo->listBox()->setVScrollBarMode( TQScrollView::Auto );
  92. m_combo->listBox()->setMinimumHeight( 100 );
  93. m_toolbar->insertWidget( Combo, 10, m_combo, 4 );
  94. m_toolbar->setItemAutoSized( Combo, true );
  95. layout->addWidget( m_view, 0, 0, 0 );
  96. layout->addWidget( m_toolbar, 1, 0, 0 );
  97. }
  98. /////////////////////////////////////////////////////////////////////////////
  99. // TQT_SLOT IMPLEMENTATIONS
  100. /////////////////////////////////////////////////////////////////////////////
  101. void Smb4KPreviewDialog::slotReceivedData( Smb4KPreviewItem *item )
  102. {
  103. // If the item is NULL or not equal to m_item,
  104. // stop right here:
  105. if ( !item || item != m_item )
  106. {
  107. return;
  108. }
  109. // Clear the icon view:
  110. m_view->clear();
  111. // The item should be equal to m_item, so we can use either of those
  112. // pointers.
  113. // Process the data:
  114. if ( !item->contents().isEmpty() )
  115. {
  116. // Generate the history.
  117. switch ( m_button_id )
  118. {
  119. case Reload: /* Really? */
  120. case Back:
  121. case Forward:
  122. {
  123. // Do not insert anything into the history if
  124. // one of these three buttons was clicked.
  125. break;
  126. }
  127. default:
  128. {
  129. m_history.append( item->location() );
  130. m_current_item = --m_history.end();
  131. break;
  132. }
  133. }
  134. // Clear the combo box, put the new history there and set the
  135. // current item:
  136. m_combo->clear();
  137. for ( TQStringList::Iterator it = m_history.begin(); it != m_history.end(); it++ )
  138. {
  139. if ( !m_combo->listBox()->findItem( *it, TQt::CaseSensitive|TQt::ExactMatch ) )
  140. {
  141. m_combo->insertItem( *it, -1 );
  142. }
  143. }
  144. m_combo->setCurrentText( *m_current_item );
  145. // Now put the contents in the icon view:
  146. for ( TQValueList<ContentsItem>::ConstIterator it = item->contents().begin();
  147. it != item->contents().end(); ++it )
  148. {
  149. switch ( (*it).first )
  150. {
  151. case Smb4KPreviewItem::File:
  152. {
  153. TDEIconViewItem *view_item = new TDEIconViewItem( m_view, (*it).second, SmallIcon( "file" ) );
  154. view_item->setKey( TQString( "[file]_%1" ).arg( (*it).second ) );
  155. break;
  156. }
  157. case Smb4KPreviewItem::Directory:
  158. {
  159. // We do not want to show the '.' and '..' directories.
  160. if ( TQString::compare( (*it).second, "." ) != 0 && TQString::compare( (*it).second, ".." ) != 0 )
  161. {
  162. TDEIconViewItem *view_item = new TDEIconViewItem( m_view, (*it).second, SmallIcon( "folder" ) );
  163. view_item->setKey( TQString( "[directory]_%1" ).arg( (*it).second ) );
  164. }
  165. break;
  166. }
  167. case Smb4KPreviewItem::HiddenFile:
  168. {
  169. if ( Smb4KSettings::previewHiddenItems() )
  170. {
  171. TDEIconViewItem *view_item = new TDEIconViewItem( m_view, (*it).second, SmallIcon( "file" ) );
  172. view_item->setKey( TQString( "[file]_%1" ).arg( (*it).second ) );
  173. }
  174. break;
  175. }
  176. case Smb4KPreviewItem::HiddenDirectory:
  177. {
  178. if ( Smb4KSettings::previewHiddenItems() &&
  179. TQString::compare( (*it).second, "." ) != 0 && TQString::compare( (*it).second, ".." ) != 0 )
  180. {
  181. TDEIconViewItem *view_item = new TDEIconViewItem( m_view, (*it).second, SmallIcon( "folder" ) );
  182. view_item->setKey( TQString( "[directory]_%1" ).arg( (*it).second ) );
  183. }
  184. break;
  185. }
  186. default:
  187. {
  188. break;
  189. }
  190. }
  191. }
  192. // Now activate or deactivate the buttons:
  193. // Activate the 'Up' button if the current address is
  194. // not equal to the base address.
  195. m_toolbar->setItemEnabled( Up, TQString::compare( "//"+item->host()+"/"+item->share()+"/", item->location() ) != 0 );
  196. // Activate/Deactivate 'Back' and 'Forward' buttons.
  197. m_toolbar->setItemEnabled( Back, m_current_item != m_history.at( 0 ) );
  198. m_toolbar->setItemEnabled( Forward, m_current_item != m_history.at( m_history.count() - 1 ) );
  199. }
  200. else
  201. {
  202. return;
  203. }
  204. }
  205. void Smb4KPreviewDialog::slotItemExecuted( TQIconViewItem *item )
  206. {
  207. if ( !item->key().startsWith( "[file]_" ) )
  208. {
  209. m_button_id = None;
  210. m_item->setPath( m_item->path()+item->text()+"/" );
  211. Smb4KCore::previewer()->preview( m_item );
  212. }
  213. }
  214. void Smb4KPreviewDialog::slotButtonClicked( int id )
  215. {
  216. m_button_id = id;
  217. m_item->clearContents();
  218. switch ( id )
  219. {
  220. case Reload:
  221. {
  222. Smb4KCore::previewer()->preview( m_item );
  223. break;
  224. }
  225. case Up:
  226. {
  227. // Do nothing if the path is empty because
  228. // we are already in the root directory.
  229. if ( m_item->path().isEmpty() )
  230. {
  231. return;
  232. }
  233. if ( m_item->path().contains( "/" ) > 1 )
  234. {
  235. m_item->setPath( m_item->path().section( "/", 0, -3 ).append( "/" ) );
  236. }
  237. else if ( m_item->path().contains( "/", true ) == 1 )
  238. {
  239. m_item->setPath( TQString() );
  240. }
  241. Smb4KCore::previewer()->preview( m_item );
  242. break;
  243. }
  244. case Back:
  245. {
  246. // Move one item back in the list:
  247. if ( m_current_item != m_history.at( 0 ) )
  248. {
  249. m_current_item--;
  250. }
  251. else
  252. {
  253. return;
  254. }
  255. // Get the path:
  256. if ( (*m_current_item).contains( "/", true ) == 3 )
  257. {
  258. m_item->setPath( TQString() );
  259. }
  260. else
  261. {
  262. m_item->setPath( (*m_current_item).section( "/", 4, -1 ) );
  263. if ( !m_item->path().isEmpty() )
  264. {
  265. m_item->setPath( m_item->path()+"/" );
  266. }
  267. else
  268. {
  269. // Do nothing.
  270. }
  271. }
  272. Smb4KCore::previewer()->preview( m_item );
  273. break;
  274. }
  275. case Forward:
  276. {
  277. // Move one item forward in the list:
  278. if ( m_current_item != m_history.at( m_history.count() - 1 ) )
  279. {
  280. m_current_item++;
  281. }
  282. else
  283. {
  284. return;
  285. }
  286. // Get the path:
  287. if ( (*m_current_item).contains( "/", true ) == 3 )
  288. {
  289. m_item->setPath( TQString() );
  290. }
  291. else
  292. {
  293. m_item->setPath( (*m_current_item).section( "/", 4, -1 ) );
  294. if ( !m_item->path().isEmpty() )
  295. {
  296. m_item->setPath( m_item->path()+"/" );
  297. }
  298. else
  299. {
  300. // Do nothing.
  301. }
  302. }
  303. Smb4KCore::previewer()->preview( m_item );
  304. break;
  305. }
  306. default:
  307. {
  308. break;
  309. }
  310. }
  311. }
  312. void Smb4KPreviewDialog::slotItemActivated( const TQString &item )
  313. {
  314. m_button_id = Combo;
  315. // First we have to strip the address:
  316. m_item->setPath( item.section( "//"+m_item->host()+"/"+m_item->share()+"/", 1, 1 ).stripWhiteSpace() );
  317. Smb4KCore::previewer()->preview( m_item );
  318. }
  319. void Smb4KPreviewDialog::slotClose()
  320. {
  321. saveDialogSize( *(Smb4KSettings::self()->config()), "PreviewDialog" );
  322. KDialogBase::slotClose();
  323. }
  324. #include "smb4kpreviewdialog.moc"