[vlc-devel] [PATCH 1/2] Use QT file browser functions which return URLs when possible.

Frank Praznik frank.praznik at gmail.com
Wed Dec 21 20:04:02 CET 2016


The getOpenFileNames() and getSaveFileName() methods of QFileDialog only work
on local paths and return a blank string if the dialog is used to select a
remote file (e.g., on a Samba share).  As of Qt 5.2 the QFileDialog class
provides the methods getOpenFileUrls() and getSaveFileUrl() that return QUrl
objects which can contain URLs to remote paths.  Use these methods when an
appropriate version of the Qt libraries are available so that the paths to
remote files are returned correctly when selected via the various file
selection dialogs.

Signed-off-by: Frank Praznik <frank.praznik at gmail.com>
---

 This fixes the issue in https://trac.videolan.org/vlc/ticket/17807

 modules/gui/qt/components/open_panels.cpp |  8 +++++-
 modules/gui/qt/dialogs_provider.cpp       | 48 ++++++++++++++++++++++++++-----
 modules/gui/qt/dialogs_provider.hpp       | 10 +++++++
 modules/gui/qt/qt.hpp                     |  1 +
 4 files changed, 59 insertions(+), 8 deletions(-)

diff --git a/modules/gui/qt/components/open_panels.cpp b/modules/gui/qt/components/open_panels.cpp
index 9f05548..08d4df0 100644
--- a/modules/gui/qt/components/open_panels.cpp
+++ b/modules/gui/qt/components/open_panels.cpp
@@ -220,7 +220,7 @@ void FileOpenPanel::dropEvent( QDropEvent *event )
 
 void FileOpenPanel::browseFile()
 {
-    QStringList files = QFileDialog::getOpenFileNames( this, qtr( "Select one or multiple files" ), p_intf->p_sys->filepath) ;
+    QStringList files = DialogsProvider::getOpenFileNames( this, qtr( "Select one or multiple files" ), p_intf->p_sys->filepath );
     foreach( const QString &file, files )
     {
         QListWidgetItem *item =
@@ -273,9 +273,15 @@ void FileOpenPanel::updateMRL()
         }
     else
     {
+#if HAS_QT52
+        QList<QUrl> urls = dialogBox->selectedUrls();
+        foreach( const QUrl &url, urls )
+            fileList.append( url.toEncoded() );
+#else
         fileList = dialogBox->selectedFiles();
         for( int i = 0; i < fileList.count(); i++ )
             fileList[i] = toURI( fileList[i] );
+#endif
     }
 
     /* Options */
diff --git a/modules/gui/qt/dialogs_provider.cpp b/modules/gui/qt/dialogs_provider.cpp
index 0d37cdb..91852f2 100644
--- a/modules/gui/qt/dialogs_provider.cpp
+++ b/modules/gui/qt/dialogs_provider.cpp
@@ -110,6 +110,40 @@ DialogsProvider::~DialogsProvider()
     delete miscPopupMenu;
 }
 
+QStringList DialogsProvider::getOpenFileNames( QWidget *parent,
+                                               const QString &caption,
+                                               const QString &dir,
+                                               const QString &filter,
+                                               QString *selectedFilter )
+{
+    QStringList files;
+
+#if HAS_QT52
+    QList<QUrl> urls = QFileDialog::getOpenFileUrls( parent, caption, QUrl::fromUserInput( dir ), filter, selectedFilter );
+
+    foreach( const QUrl url, urls )
+        files.append( url.toEncoded() );
+#else
+    files = QFileDialog::getOpenFileNames( parent, caption, dir, filter, selectedFilter );
+#endif
+
+    return files;
+}
+
+QString DialogsProvider::getSaveFileName( QWidget *parent,
+                                          const QString &caption,
+                                          const QString &dir,
+                                          const QString &filter,
+                                          QString *selectedFilter )
+{
+#if HAS_QT52
+    QUrl url = QFileDialog::getSaveFileUrl( parent, caption, QUrl::fromUserInput( dir ), filter, selectedFilter );
+    return QString( url.toEncoded() );
+#else
+    return QFileDialog::getSaveFileName( parent, caption, dir, filter, selectedFilter );
+#endif
+}
+
 void DialogsProvider::quit()
 {
     b_isDying = true;
@@ -349,7 +383,7 @@ void DialogsProvider::openFileGenericDialog( intf_dialog_args_t *p_arg )
     /* Save */
     if( p_arg->b_save )
     {
-        QString file = QFileDialog::getSaveFileName( NULL,
+        QString file = getSaveFileName( NULL,
                                         qfu( p_arg->psz_title ),
                                         p_intf->p_sys->filepath, extensions );
         if( !file.isEmpty() )
@@ -363,7 +397,7 @@ void DialogsProvider::openFileGenericDialog( intf_dialog_args_t *p_arg )
     }
     else /* non-save mode */
     {
-        QStringList files = QFileDialog::getOpenFileNames( NULL,
+        QStringList files = getOpenFileNames( NULL,
                 qfu( p_arg->psz_title ), p_intf->p_sys->filepath,
                 extensions );
         p_arg->i_results = files.count();
@@ -462,7 +496,7 @@ QStringList DialogsProvider::showSimpleOpen( const QString& help,
     ADD_EXT_FILTER( fileTypes, EXTENSIONS_ALL );
     fileTypes.replace( ";*", " *");
 
-    QStringList files = QFileDialog::getOpenFileNames( NULL,
+    QStringList files = getOpenFileNames( NULL,
         help.isEmpty() ? qtr(I_OP_SEL_FILES ) : help,
         path.isEmpty() ? p_intf->p_sys->filepath : path,
         fileTypes );
@@ -616,10 +650,10 @@ void DialogsProvider::saveAPlaylist(playlist_t *p_playlist, playlist_item_t *p_n
     }
 
     QString selected;
-    QString file = QFileDialog::getSaveFileName( NULL,
-                                                 qtr( "Save playlist as..." ),
-                                                 p_intf->p_sys->filepath, filters.join( ";;" ),
-                                                 &selected );
+    QString file = getSaveFileName( NULL,
+                                    qtr( "Save playlist as..." ),
+                                    p_intf->p_sys->filepath, filters.join( ";;" ),
+                                    &selected );
     const char *psz_selected_module = NULL;
     const char *psz_last_playlist_ext = NULL;
 
diff --git a/modules/gui/qt/dialogs_provider.hpp b/modules/gui/qt/dialogs_provider.hpp
index 36a41ea..b72f8ec 100644
--- a/modules/gui/qt/dialogs_provider.hpp
+++ b/modules/gui/qt/dialogs_provider.hpp
@@ -91,6 +91,16 @@ public:
                                 const QString& path = QString() );
     bool isDying() { return b_isDying; }
     static QString getDirectoryDialog( intf_thread_t *p_intf);
+    static QStringList getOpenFileNames( QWidget *parent = Q_NULLPTR,
+                                         const QString &caption = QString(),
+                                         const QString &dir = QString(),
+                                         const QString &filter = QString(),
+                                         QString *selectedFilter = Q_NULLPTR );
+    static QString getSaveFileName( QWidget *parent = Q_NULLPTR,
+                                    const QString &caption = QString(),
+                                    const QString &dir = QString(),
+                                    const QString &filter = QString(),
+                                    QString *selectedFilter = Q_NULLPTR );
 
 protected:
     QSignalMapper *menusMapper;
diff --git a/modules/gui/qt/qt.hpp b/modules/gui/qt/qt.hpp
index 99ce132..b666cbb 100644
--- a/modules/gui/qt/qt.hpp
+++ b/modules/gui/qt/qt.hpp
@@ -47,6 +47,7 @@
 #endif
 
 #define HAS_QT5  ( QT_VERSION >= 0x050000 )
+#define HAS_QT52 ( QT_VERSION >= 0x050200 )
 #define HAS_QT56 ( QT_VERSION >= 0x050600 )
 
 /* Q_DECL_OVERRIDE is a Qt5 feature, add empty define to not break with Qt4 */
-- 
2.9.3



More information about the vlc-devel mailing list