1 //////////////////////////////////////////////////////////////////////
2 //
3 // BeeBEEP Copyright (C) 2010-2021 Marco Mastroddi
4 //
5 // BeeBEEP is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published
7 // by the Free Software Foundation, either version 3 of the License,
8 // or (at your option) any later version.
9 //
10 // BeeBEEP is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with BeeBEEP. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // Author: Marco Mastroddi <marco.mastroddi(AT)gmail.com>
19 //
20 // $Id: GuiShareBox.cpp 1455 2020-12-23 10:17:53Z mastroddi $
21 //
22 //////////////////////////////////////////////////////////////////////
23 
24 #include "BeeUtils.h"
25 #include "BeeApplication.h"
26 #include "GuiIconProvider.h"
27 #include "GuiShareBox.h"
28 #include "GuiShareBoxFileInfoList.h"
29 #include "IconManager.h"
30 #include "Settings.h"
31 #include "FileDialog.h"
32 #include "UserManager.h"
33 
34 
GuiShareBox(QWidget * parent)35 GuiShareBox::GuiShareBox( QWidget *parent )
36   : QWidget( parent )
37 {
38   setupUi( this );
39   setObjectName( "GuiShareBox" );
40 
41   QGridLayout* grid_layout = new QGridLayout( this );
42   grid_layout->setSpacing( 0 );
43   grid_layout->setObjectName( QString::fromUtf8( "grid_layout" ) );
44   grid_layout->setContentsMargins( 0, 0, 0, 0 );
45 
46   grid_layout->addWidget( mp_lTitle, 0, 0, 1, 1 );
47 
48   mp_splitter = new QSplitter( this );
49   mp_splitter->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
50   mp_splitter->setOrientation( Qt::Horizontal );
51   mp_splitter->setChildrenCollapsible( false );
52   mp_splitter->addWidget( mp_myFrame );
53   mp_splitter->addWidget( mp_outFrame );
54   grid_layout->addWidget( mp_splitter, 1, 0, 1, 1);
55 
56   QString window_title = QString( "<b>%1</b>" ).arg( QString( "BeeBOX" ) );
57   mp_lTitle->setText( window_title );
58 
59   mp_myBox->initTree();
60   mp_outBox->initTree();
61 
62   m_myCurrentFolder = "";
63   m_outCurrentFolder = "";
64   m_userId = ID_INVALID;
65   mp_fsWatcher = beeApp->fsWatcher();
66 
67   mp_pbMyUpdate->setToolTip( tr( "Update your BeeBOX" ) );
68   mp_pbMyCreateFolder->setToolTip( tr( "Create folder in your BeeBOX" ) );
69   mp_pbMyOpenFolder->setToolTip( tr( "Show current folder" ) );
70   mp_pbOutUpdate->setToolTip( tr( "Update BeeBOX" ) );
71   mp_pbOutParentFolder->setToolTip( tr( "Back to parent folder" ) );
72   mp_pbOutCreateFolder->setToolTip( tr( "Create folder in BeeBOX" ) );
73 
74   mp_pbSelectMyBox->setIcon( IconManager::instance().icon( "sharebox.png" ) );
75   mp_pbMyUpdate->setIcon( IconManager::instance().icon( "update.png" ) );
76   mp_pbOutUpdate->setIcon( IconManager::instance().icon( "update.png" ) );
77   mp_pbOutParentFolder->setIcon( IconManager::instance().icon( "back.png" ) );
78   mp_pbMyCreateFolder->setIcon( IconManager::instance().icon( "folder-add.png" ) );
79   mp_pbOutCreateFolder->setIcon( IconManager::instance().icon( "folder-add.png" ) );
80   mp_pbMyOpenFolder->setIcon( IconManager::instance().icon( "folder.png" ) );
81 
82   mp_cbEnableMyBox->setChecked( Settings::instance().useShareBox() );
83 
84   connect( mp_comboUsers, SIGNAL( currentIndexChanged( int ) ), this, SLOT( onShareBoxSelected( int ) ) );
85   connect( mp_pbSelectMyBox, SIGNAL( clicked() ), this, SLOT( selectMyShareBoxFolder() ) );
86   connect( mp_cbEnableMyBox, SIGNAL( toggled( bool ) ), this, SLOT( onEnableMyShareBoxClicked() ) );
87   connect( mp_pbMyUpdate, SIGNAL( clicked() ), this, SLOT( updateMyBox() ) );
88   connect( mp_pbOutUpdate, SIGNAL( clicked() ), this, SLOT( updateOutBox() ) );
89   connect( mp_pbOutParentFolder, SIGNAL( clicked() ), this, SLOT( backToParentFolder() ) );
90   connect( mp_myBox, SIGNAL( itemDoubleClicked( QTreeWidgetItem*, int ) ), this, SLOT( onMyItemDoubleClicked( QTreeWidgetItem*, int ) ) );
91   connect( mp_outBox, SIGNAL( itemDoubleClicked( QTreeWidgetItem*, int ) ), this, SLOT( onOutItemDoubleClicked( QTreeWidgetItem*, int ) ) );
92   connect( mp_myBox, SIGNAL( dropEventRequest( const QString& ) ), this, SLOT( dropInMyBox( const QString& ) ) );
93   connect( mp_outBox, SIGNAL( dropEventRequest( const QString& ) ), this, SLOT( dropInOutBox( const QString& ) ) );
94   connect( mp_pbMyCreateFolder, SIGNAL( clicked() ), this, SLOT( createFolderInMyBox() ) );
95   connect( mp_pbOutCreateFolder, SIGNAL( clicked() ), this, SLOT( createFolderInOutBox() ) );
96   connect( mp_pbMyOpenFolder, SIGNAL( clicked() ), this, SLOT( openMyBox() ) );
97   connect( mp_fsWatcher, SIGNAL( directoryChanged( const QString& ) ), this, SLOT( updateFolder( const QString& ) ) );
98 }
99 
onEnableMyShareBoxClicked()100 void GuiShareBox::onEnableMyShareBoxClicked()
101 {
102   Settings::instance().setUseShareBox( mp_cbEnableMyBox->isChecked() );
103   setCurrentFolder( ID_LOCAL_USER, "" );
104   bool select_box = false;
105   mp_comboUsers->setEnabled( Settings::instance().useShareBox() );
106   mp_lUsers->setEnabled( mp_comboUsers->isEnabled() );
107 
108   if( Settings::instance().useShareBox() )
109   {
110     select_box = Settings::instance().shareBoxPath().isEmpty();
111     if( !select_box )
112     {
113       QDir box_folder( Settings::instance().shareBoxPath() );
114       if( box_folder.exists() )
115         select_box = !Bee::folderIsWriteable( Settings::instance().shareBoxPath(), false );
116     }
117   }
118 
119   if( select_box )
120   {
121     QMessageBox::information( this, Settings::instance().programName(), tr( "BeeBOX path does not exist. Please select a valid folder.") );
122     selectMyShareBoxFolder();
123   }
124   else
125   {
126     Settings::instance().save();
127     QTimer::singleShot( 100, this, SLOT( updateShareBoxes() ) );
128   }
129 }
130 
selectMyShareBoxFolder()131 void GuiShareBox::selectMyShareBoxFolder()
132 {
133   QString folder_path = FileDialog::getExistingDirectory( this,
134                                                           tr( "%1 - Select the BeeBOX folder" )
135                                                           .arg( Settings::instance().programName() ),
136                                                                 Settings::instance().shareBoxPath() );
137   if( folder_path.isEmpty() )
138     return;
139 
140   Settings::instance().setShareBoxPath( Bee::convertToNativeFolderSeparator( folder_path ) );
141   Settings::instance().save();
142   makeShareBoxRequest( ID_LOCAL_USER, "", false );
143 }
144 
setUsers()145 void GuiShareBox::setUsers()
146 {
147   mp_comboUsers->clear();
148   mp_comboUsers->addItem( QString( "..." ), static_cast<VNumber>(ID_INVALID) );
149   foreach( User u, UserManager::instance().userList().toList() )
150   {
151     if( u.isStatusConnected() )
152       mp_comboUsers->addItem( Bee::userNameToShow( u, false ), u.id() );
153   }
154   mp_comboUsers->setEnabled( Settings::instance().useShareBox() );
155   mp_lUsers->setEnabled( mp_comboUsers->isEnabled() );
156 }
157 
selectedUserId() const158 VNumber GuiShareBox::selectedUserId() const
159 {
160   VNumber user_id;
161 #if QT_VERSION >= 0x050000
162   user_id = Bee::qVariantToVNumber( mp_comboUsers->currentData() );
163 #else
164   user_id = Bee::qVariantToVNumber( mp_comboUsers->itemData( mp_comboUsers->currentIndex() ) );
165 #endif
166   return user_id;
167 }
168 
updateShareBoxes()169 void GuiShareBox::updateShareBoxes()
170 {
171   setUsers();
172   makeShareBoxRequest( ID_LOCAL_USER, "", false );
173   makeShareBoxRequest( selectedUserId(), "", false );
174 }
175 
updateCurrentFolderLabel(VNumber user_id)176 void GuiShareBox::updateCurrentFolderLabel( VNumber user_id )
177 {
178   QLabel* folder_label = currentFolderLabel( user_id );
179 
180   if( user_id == ID_LOCAL_USER )
181   {
182     if( Settings::instance().useShareBox() )
183     {
184       folder_label->setText( Bee::convertToNativeFolderSeparator( QString( "<b>./%1</b>" ).arg( m_myCurrentFolder ) ) );
185       folder_label->setToolTip( QString( "%1%2%3" ).arg( Settings::instance().shareBoxPath() ).arg( QDir::separator() ).arg( m_myCurrentFolder ) );
186     }
187     else
188     {
189       folder_label->setText( "" );
190       folder_label->setToolTip( "" );
191     }
192   }
193   else
194   {
195     if( Settings::instance().useShareBox() )
196       folder_label->setText( Bee::convertToNativeFolderSeparator( QString( "<b>./%1</b>" ).arg( m_outCurrentFolder ) ) );
197     else
198       folder_label->setText( "" );
199   }
200 }
201 
setCurrentFolder(VNumber user_id,const QString & new_path)202 void GuiShareBox::setCurrentFolder( VNumber user_id, const QString& new_path )
203 {
204   if( user_id != ID_LOCAL_USER )
205   {
206     m_userId = user_id;
207     if( m_userId != ID_INVALID )
208       m_outCurrentFolder = new_path;
209     else
210       m_outCurrentFolder = "";
211   }
212   else
213   {
214     if( Settings::instance().useShareBox() )
215     {
216       QString old_dir_to_watch = Bee::convertToNativeFolderSeparator( m_myCurrentFolder.isEmpty() ? Settings::instance().shareBoxPath() : QString( "%1/%2" ).arg( Settings::instance().shareBoxPath(), m_myCurrentFolder ) );
217       QString new_dir_to_watch = Bee::convertToNativeFolderSeparator( new_path.isEmpty() ? Settings::instance().shareBoxPath() : QString( "%1/%2" ).arg( Settings::instance().shareBoxPath(), new_path ) );
218       mp_fsWatcher->removePath( old_dir_to_watch );
219       mp_fsWatcher->addPath( new_dir_to_watch );
220 #ifdef BEEBEEP_DEBUG
221       qDebug() << "FileSystemWatcher is now checking path" << qPrintable( mp_fsWatcher->directories().join( ", " ) );
222 #endif
223     }
224     else
225     {
226       QStringList fsw_dirs = mp_fsWatcher->directories();
227       if( !fsw_dirs.isEmpty() )
228       {
229 #ifdef BEEBEEP_DEBUG
230         qDebug() << "FileSystemWatcher does not check path" << qPrintable( fsw_dirs.join( ", " ) ) << "anymore";
231 #endif
232         mp_fsWatcher->removePaths( fsw_dirs );
233       }
234     }
235     m_myCurrentFolder = new_path;
236   }
237 }
238 
disableBox(VNumber user_id)239 void GuiShareBox::disableBox( VNumber user_id )
240 {
241   GuiShareBoxFileInfoList* pfil = fileInfoList( user_id );
242   pfil->clearTree();
243   pfil->setToolTip( "" );
244   pfil->setEnabled( false );
245   if( user_id == ID_LOCAL_USER )
246   {
247     mp_lMyBox->setEnabled( false );
248     mp_lMyBox->setToolTip( "" );
249     mp_pbMyCreateFolder->setEnabled( false );
250     mp_pbMyUpdate->setEnabled( false );
251     mp_pbMyOpenFolder->setEnabled( false );
252 
253   }
254   else
255   {
256     mp_lOutBox->setEnabled( false );
257     mp_pbOutParentFolder->setEnabled( false );
258     mp_pbOutUpdate->setEnabled( false );
259     mp_pbOutCreateFolder->setEnabled( false );
260   }
261 }
262 
enableBox(VNumber user_id)263 void GuiShareBox::enableBox( VNumber user_id )
264 {
265   GuiShareBoxFileInfoList* pfil = fileInfoList( user_id );
266   pfil->setToolTip( "" );
267   pfil->setEnabled( true );
268 
269   if( user_id == ID_LOCAL_USER )
270   {
271     mp_pbMyCreateFolder->setEnabled( true );
272     mp_pbMyOpenFolder->setEnabled( true );
273     mp_pbMyUpdate->setEnabled( false );
274     mp_lMyBox->setEnabled( true );
275   }
276   else
277   {
278     mp_pbOutParentFolder->setEnabled( !m_outCurrentFolder.isEmpty() );
279     mp_pbOutUpdate->setEnabled( true );
280     mp_pbOutCreateFolder->setEnabled( true );
281     mp_lOutBox->setEnabled( true );
282   }
283 }
284 
updateMyBox()285 void GuiShareBox::updateMyBox()
286 {
287   if( Settings::instance().useShareBox() )
288     makeShareBoxRequest( ID_LOCAL_USER, m_myCurrentFolder, false );
289   else
290     disableBox( ID_LOCAL_USER );
291 }
292 
updateOutBox()293 void GuiShareBox::updateOutBox()
294 {
295   VNumber user_id = selectedUserId();
296   if( Settings::instance().useShareBox() )
297   {
298     if( user_id != m_userId )
299       makeShareBoxRequest( user_id, "", false );
300     else
301       makeShareBoxRequest( user_id, m_outCurrentFolder, false );
302   }
303   else
304     disableBox( user_id );
305 }
306 
updateBox(const User & u,const QString & folder_path,const QList<FileInfo> & file_info_list)307 void GuiShareBox::updateBox( const User& u, const QString& folder_path, const QList<FileInfo>& file_info_list )
308 {
309   if( u.isLocal() )
310   {
311     if( mp_cbEnableMyBox->isChecked() )
312       updateMyBox( folder_path, file_info_list );
313     else
314       disableBox( ID_LOCAL_USER );
315   }
316   else
317   {
318     if( mp_cbEnableMyBox->isChecked() )
319       updateOutBox( u, folder_path, file_info_list );
320     else
321       disableBox( u.id() );
322   }
323 }
324 
updateMyBox(const QString & folder_path,const QList<FileInfo> & file_info_list)325 void GuiShareBox::updateMyBox( const QString& folder_path, const QList<FileInfo>& file_info_list )
326 {
327   enableBox( ID_LOCAL_USER );
328   mp_myBox->setFileInfoList( file_info_list );
329   if( !folder_path.isEmpty() )
330     mp_myBox->addDotDotFolder();
331   updateCurrentFolderLabel( ID_LOCAL_USER );
332 }
333 
updateOutBox(const User & u,const QString & folder_path,const QList<FileInfo> & file_info_list)334 void GuiShareBox::updateOutBox( const User& u, const QString& folder_path, const QList<FileInfo>& file_info_list )
335 {
336   if( m_userId != u.id() )
337   {
338 #ifdef BEEBEEP_DEBUG
339     qDebug() << "GuiSharebox skips updates for not current user" << qPrintable( u.name() );
340 #endif
341     return;
342   }
343 
344   if( !u.isStatusConnected() )
345   {
346     m_outCurrentFolder = "";
347     updateCurrentFolderLabel( m_userId );
348     return;
349   }
350 
351   if( m_outCurrentFolder != folder_path )
352   {
353 #ifdef BEEBEEP_DEBUG
354     qDebug() << "GuiSharebox skips updates from" << qPrintable( u.name() ) << "for not current folder" << qPrintable( folder_path );
355 #endif
356     return;
357   }
358 
359   mp_outBox->setFileInfoList( file_info_list );
360   if( !folder_path.isEmpty() )
361     mp_outBox->addDotDotFolder();
362 
363   enableBox( m_userId );
364   updateCurrentFolderLabel( m_userId );
365 }
366 
onMyItemDoubleClicked(QTreeWidgetItem * item,int)367 void GuiShareBox::onMyItemDoubleClicked( QTreeWidgetItem* item, int )
368 {
369   if( !item )
370     return;
371 
372   GuiShareBoxFileInfoItem* file_info_item = dynamic_cast<GuiShareBoxFileInfoItem*>(item);
373 
374   if( file_info_item->isFolder() )
375   {
376     QString new_folder;
377     if( file_info_item->fileInfo().id() == ID_DOTDOT_FOLDER )
378       new_folder = Bee::folderCdUp( m_myCurrentFolder );
379     else if( m_myCurrentFolder.isEmpty() )
380       new_folder = QString( "%1" ).arg( file_info_item->fileInfo().name() );
381     else
382       new_folder = QString( "%1%2%3" ).arg( m_myCurrentFolder ).arg( QDir::separator() ).arg( file_info_item->fileInfo().name() );
383     makeShareBoxRequest( ID_LOCAL_USER, new_folder, false );
384   }
385   else
386   {
387     QUrl file_url = QUrl::fromLocalFile( file_info_item->fileInfo().path() );
388     emit openUrlRequest( file_url );
389   }
390 }
391 
onOutItemDoubleClicked(QTreeWidgetItem * item,int)392 void GuiShareBox::onOutItemDoubleClicked( QTreeWidgetItem* item, int )
393 {
394   if( !item )
395     return;
396 
397   GuiShareBoxFileInfoItem* file_info_item = dynamic_cast<GuiShareBoxFileInfoItem*>(item);
398 
399   if( file_info_item->isFolder() )
400   {
401     QString new_folder;
402     if( file_info_item->fileInfo().id() == ID_DOTDOT_FOLDER )
403       new_folder = Bee::folderCdUp( m_outCurrentFolder );
404     else if( m_outCurrentFolder.isEmpty() )
405       new_folder = QString( "%1" ).arg( file_info_item->fileInfo().name() );
406     else
407       new_folder = QString( "%1%2%3" ).arg( m_outCurrentFolder ).arg( QDir::separator() ).arg( file_info_item->fileInfo().name() );
408     makeShareBoxRequest( m_userId, new_folder, false );
409   }
410 }
411 
onShareBoxSelected(int)412 void GuiShareBox::onShareBoxSelected( int )
413 {
414   VNumber current_user_id = selectedUserId();
415   if( current_user_id > ID_LOCAL_USER )
416   {
417 #ifdef BEEBEEP_DEBUG
418     qDebug() << "BeeBOX requests list for user" << current_user_id;
419 #endif
420     makeShareBoxRequest( current_user_id, current_user_id == m_userId ? m_outCurrentFolder : "", false );
421   }
422   else
423   {
424     setCurrentFolder( current_user_id, "" );
425     disableBox( current_user_id );
426     updateCurrentFolderLabel( current_user_id );
427   }
428 }
429 
onShareFolderUnavailable(const User & u,const QString & folder_path)430 void GuiShareBox::onShareFolderUnavailable( const User& u, const QString& folder_path )
431 {
432 #ifdef BEEBEEP_DEBUG
433   qDebug() << qPrintable( u.path() ) << "has not shared box folder" << qPrintable( folder_path );
434 #endif
435   if( u.isLocal() )
436   {
437     if( mp_cbEnableMyBox->isChecked() )
438       QMessageBox::information( this, Settings::instance().programName(), tr( "%1: access denied." ).arg( folder_path ) );
439   }
440   else
441   {
442     if( u.id() != m_userId )
443       return;
444 
445     if( folder_path != m_outCurrentFolder )
446       return;
447 
448     mp_lOutBox->setText( tr( "%1 <b>%2</b>" ).arg( folder_path.isEmpty() ? QString( "BeeBOX" ) : folder_path ).arg( tr( "is unavailable" ) ) );
449     mp_outBox->setToolTip( Bee::removeHtmlTags( mp_lOutBox->text() ) );
450   }
451 
452   GuiShareBoxFileInfoList* fil = fileInfoList( u.id() );
453   if( fil->isEnabled() )
454     disableBox( u.id() );
455 }
456 
makeShareBoxRequest(VNumber user_id,const QString & folder_path,bool create_folder)457 void GuiShareBox::makeShareBoxRequest( VNumber user_id, const QString& folder_path, bool create_folder )
458 {
459   disableBox( user_id );
460   if( user_id == ID_INVALID )
461     return;
462   setCurrentFolder( user_id, folder_path );
463   QLabel* current_folder_label = currentFolderLabel( user_id );
464 
465   if( Settings::instance().useShareBox() )
466   {
467 #ifdef BEEBEEP_DEBUG
468     qDebug() << "BeeBOX ask for updating folder" << folder_path << "of user" << user_id;
469 #endif
470     GuiShareBoxFileInfoList* pfil = fileInfoList( user_id );
471     if( folder_path.isEmpty() )
472       pfil->setToolTip( tr( "Please wait" ) );
473     else
474       pfil->setToolTip( tr( "Please wait for path %1" ).arg( folder_path ) );
475 
476     current_folder_label->setText( QString( "<b>%1...</b>" ).arg( tr( "Please wait" ) ) );
477     current_folder_label->setToolTip( pfil->toolTip() );
478 
479     emit shareBoxRequest( user_id, folder_path, create_folder );
480   }
481   else
482     current_folder_label->setText( QString( "<b>%1</b>" ).arg( tr( "Disabled" ) ) );
483 }
484 
updateUser(const User & u)485 void GuiShareBox::updateUser( const User& u )
486 {
487   if( u.isLocal() )
488     return;
489 
490   int user_index = mp_comboUsers->findData( u.id() );
491   if( user_index >= 0 )
492   {
493     if( !u.isStatusConnected() )
494     {
495       if( mp_comboUsers->currentIndex() == user_index )
496       {
497 #ifdef BEEBEEP_DEBUG
498         qDebug() << qPrintable( u.path() ) << "is offline and ShareBox will be disabled";
499 #endif
500         m_outCurrentFolder = "";
501         onShareFolderUnavailable( u, m_outCurrentFolder );
502       }
503       mp_comboUsers->removeItem( user_index );
504     }
505     else
506       mp_comboUsers->setItemText( user_index, Bee::userNameToShow( u, false ) );
507   }
508   else
509   {
510     if( u.isStatusConnected() )
511       mp_comboUsers->addItem( Bee::userNameToShow( u, false ), u.id() );
512   }
513 }
514 
dropInMyBox(const QString & share_path)515 void GuiShareBox::dropInMyBox( const QString& share_path )
516 {
517   QStringList sl_paths = share_path.split( "\n" );
518   QList<FileInfo> selected_list = mp_outBox->selectedFileInfoList();
519   if( sl_paths.size() != selected_list.size() )
520     qWarning() << "BeeBOX (mybox) has found drop list size" << sl_paths.size() << "not equal to selected list size" << selected_list.size();
521 
522   foreach( FileInfo file_info, selected_list )
523   {
524 #ifdef BEEBEEP_DEBUG
525     QString from_path = file_info.shareFolder().isEmpty() ? file_info.name() : QString( "%1%2%3" ).arg( file_info.shareFolder() ).arg( QDir::separator() ).arg( file_info.name() );
526     qDebug() << "Drop in MY BeeBOX the file" << file_info.name() << "->" << from_path;
527 #endif
528     QString to_path;
529     if( m_myCurrentFolder.isEmpty() )
530       to_path = Bee::convertToNativeFolderSeparator( QString( "%1/%2" ).arg( Settings::instance().shareBoxPath(), file_info.name() ) );
531     else
532       to_path = Bee::convertToNativeFolderSeparator( QString( "%1/%2/%3" ).arg( Settings::instance().shareBoxPath(), m_myCurrentFolder, file_info.name() ) );
533 
534     emit shareBoxDownloadRequest( m_userId, file_info, to_path );
535   }
536 }
537 
dropInOutBox(const QString & share_path)538 void GuiShareBox::dropInOutBox( const QString& share_path )
539 {
540   QStringList sl_paths = share_path.split( "\n" );
541   QList<FileInfo> selected_list = mp_myBox->selectedFileInfoList();
542   if( sl_paths.size() != selected_list.size() )
543     qWarning() << "BeeBOX (outbox) has found drop list size" << sl_paths.size() << "not equal to selected list size" << selected_list.size();
544 
545   foreach( FileInfo file_info, selected_list )
546   {
547     QString to_path = Bee::convertToNativeFolderSeparator( m_outCurrentFolder );
548 #ifdef BEEBEEP_DEBUG
549     qDebug() << "Drop in OUT BeeBOX the file" << file_info.name() << "->" << to_path;
550 #endif
551     emit shareBoxUploadRequest( m_userId, file_info, to_path );
552   }
553 }
554 
onFileUploadCompleted(VNumber user_id,const FileInfo & fi)555 void GuiShareBox::onFileUploadCompleted( VNumber user_id, const FileInfo& fi )
556 {
557 #ifdef BEEBEEP_DEBUG
558   qDebug() << "BeeBOX (upload completed) update list of the folder" << fi.shareFolder() << "and user" << user_id << "and current folder is" << m_outCurrentFolder << "of user" << m_userId;
559 #endif
560   if( m_userId == user_id && fi.shareFolder() == m_outCurrentFolder )
561     updateOutBox();
562 }
563 
onFileDownloadCompleted(VNumber user_id,const FileInfo & fi)564 void GuiShareBox::onFileDownloadCompleted( VNumber user_id, const FileInfo& fi )
565 {
566 #ifdef BEEBEEP_DEBUG
567   qDebug() << "BeeBOX (download completed) update list of the folder" << fi.shareFolder() << "and user" << user_id << "and current folder is" << m_outCurrentFolder << "of user" << m_userId;
568 #else
569   Q_UNUSED( user_id );
570   Q_UNUSED( fi );
571 #endif
572   updateMyBox();
573 }
574 
isValidNewFolderName(QTreeWidget * tw,const QString & name_to_check)575 bool GuiShareBox::isValidNewFolderName( QTreeWidget* tw, const QString& name_to_check )
576 {
577   QTreeWidgetItemIterator it( tw );
578   while( *it )
579   {
580     if( (*it)->text( GuiShareBoxFileInfoItem::ColumnFile ).toLower() == name_to_check.toLower() )
581       return false;
582     ++it;
583   }
584   return true;
585 }
586 
createFolderInBox(VNumber user_id)587 void GuiShareBox::createFolderInBox( VNumber user_id )
588 {
589   if( user_id == ID_INVALID )
590     return;
591 
592   GuiShareBoxFileInfoList* tw = fileInfoList( user_id );
593   if( !tw->isEnabled() )
594     return;
595 
596   QString current_folder = currentFolder( user_id );
597 
598   QString folder_to_create = QInputDialog::getText( tw, Settings::instance().programName(), tr( "Please insert the new folder name" ) );
599   if( folder_to_create.isEmpty() )
600     return;
601 
602   if( !isValidNewFolderName( tw, folder_to_create ) )
603   {
604     QMessageBox::information( tw, Settings::instance().programName(), tr( "%1 already exists." ).arg( folder_to_create ) );
605     return;
606   }
607 
608   QString new_folder_path = current_folder.isEmpty() ? folder_to_create : QString( "%1%2%3" ).arg( current_folder ).arg( QDir::separator() ).arg( folder_to_create );
609   makeShareBoxRequest( user_id, new_folder_path, true );
610 }
611 
createFolderInMyBox()612 void GuiShareBox::createFolderInMyBox()
613 {
614   createFolderInBox( ID_LOCAL_USER );
615 }
616 
createFolderInOutBox()617 void GuiShareBox::createFolderInOutBox()
618 {
619   createFolderInBox( m_userId );
620 }
621 
openMyBox()622 void GuiShareBox::openMyBox()
623 {
624   QString folder_path = m_myCurrentFolder.isEmpty() ? Settings::instance().shareBoxPath() : QString( "%1%2%3" ).arg( Settings::instance().shareBoxPath() ).arg( QDir::separator() ).arg( m_myCurrentFolder );
625   if( folder_path.isEmpty() )
626   {
627     mp_pbMyOpenFolder->setEnabled( false );
628     return;
629   }
630   emit openUrlRequest( QUrl::fromLocalFile( folder_path ) );
631 }
632 
backToParentFolder()633 void GuiShareBox::backToParentFolder()
634 {
635   if( !m_outCurrentFolder.isEmpty() )
636   {
637     QString new_folder = Bee::folderCdUp( m_outCurrentFolder );
638     makeShareBoxRequest( m_userId, new_folder, false );
639   }
640   else
641     mp_pbOutParentFolder->setEnabled( false );
642 }
643 
updateFolder(const QString & f)644 void GuiShareBox::updateFolder( const QString& f )
645 {
646   if( Settings::instance().useShareBox() )
647   {
648 #ifdef BEEBEEP_DEBUG
649     qDebug() << "Folder" << qPrintable( f ) << "changed -> updating ShareBox";
650 #else
651     Q_UNUSED( f );
652 #endif
653     mp_pbMyUpdate->setEnabled( true );
654   }
655 }
656