[vlc-devel] [PATCH] Implemented a TODO Import/Export feature in Bookmarks dialog

Abel Tesfaye abeltesfaye45 at gmail.com
Tue Apr 2 16:49:17 CEST 2019


I found an un-implemented function  in the bookmarks dialog(made by Antoine Lejeune: phytos ~11 years ago) which I guess is supposed to export the bookmarks to a file. I implemented the function plus added an import feature.
---
 modules/gui/qt/dialogs/bookmarks.cpp | 171 ++++++++++++++++++++++++++++++++---
 modules/gui/qt/dialogs/bookmarks.hpp |   6 +-
 2 files changed, 164 insertions(+), 13 deletions(-)

diff --git a/modules/gui/qt/dialogs/bookmarks.cpp b/modules/gui/qt/dialogs/bookmarks.cpp
index 78d50b3..85ac73b 100644
--- a/modules/gui/qt/dialogs/bookmarks.cpp
+++ b/modules/gui/qt/dialogs/bookmarks.cpp
@@ -32,6 +32,13 @@
 #include <QPushButton>
 #include <QDialogButtonBox>
 #include <QModelIndexList>
+#include <QFile>
+#include <QFileDialog>
+#include <QJsonArray>
+#include <QJsonDocument>
+#include <QJsonObject>
+#include <QJsonValue>
+#include <QMessageBox>
 
 BookmarksDialog::BookmarksDialog( intf_thread_t *_p_intf ):QVLCFrame( _p_intf )
 {
@@ -53,11 +60,15 @@ BookmarksDialog::BookmarksDialog( intf_thread_t *_p_intf ):QVLCFrame( _p_intf )
     clearButton = new QPushButton( qtr( "Clear" ) );
     clearButton->setToolTip( qtr( "Delete all the bookmarks" ) );
     buttonsBox->addButton( clearButton, QDialogButtonBox::ResetRole );
-#if 0
-    QPushButton *extractButton = new QPushButton( qtr( "Extract" ) );
-    extractButton->setToolTip( qtr() );
-    buttonsBox->addButton( extractButton, QDialogButtonBox::ActionRole );
-#endif
+    exportButton = new QPushButton(qtr("Export"));
+    exportButton->setToolTip(
+        qtr("Exports all bookmarks into a VLCBookmark file"));
+    buttonsBox->addButton(exportButton, QDialogButtonBox::ResetRole);
+    importButton = new QPushButton(qtr("Import"));
+    importButton->setToolTip(qtr("Imports bookmarks from a VLCBookmark file"));
+    buttonsBox->addButton(importButton, QDialogButtonBox::ResetRole);
+
+
     /* ?? Feels strange as Qt guidelines will put reject on top */
     buttonsBox->addButton( new QPushButton( qtr( "&Close" ) ),
                           QDialogButtonBox::RejectRole);
@@ -96,10 +107,9 @@ BookmarksDialog::BookmarksDialog( intf_thread_t *_p_intf ):QVLCFrame( _p_intf )
     BUTTONACT( addButton, add() );
     BUTTONACT( delButton, del() );
     BUTTONACT( clearButton, clear() );
+    BUTTONACT(exportButton, exportBookmarks());
+    BUTTONACT(importButton, importBookmarks());
 
-#if 0
-    BUTTONACT( extractButton, extract() );
-#endif
     CONNECT( buttonsBox, rejected(), this, close() );
     updateButtons();
 
@@ -114,8 +124,11 @@ BookmarksDialog::~BookmarksDialog()
 
 void BookmarksDialog::updateButtons()
 {
-    clearButton->setEnabled( bookmarksList->model()->rowCount() > 0 );
-    delButton->setEnabled( bookmarksList->selectionModel()->hasSelection() );
+    bool isListPopulated = bookmarksList->model()->rowCount() > 0;
+    clearButton->setEnabled(isListPopulated);
+    exportButton->setEnabled(isListPopulated);
+
+    delButton->setEnabled(bookmarksList->selectionModel()->hasSelection());
 }
 
 void BookmarksDialog::update()
@@ -266,9 +279,143 @@ clear:
     free( pp_bookmarks );
 }
 
-void BookmarksDialog::extract()
+void BookmarksDialog::alert(QString msg)
 {
-    // TODO
+    QMessageBox Msgbox;
+    Msgbox.setText(msg);
+    Msgbox.exec();
+}
+
+void BookmarksDialog::importBookmarks()
+{
+    input_thread_t *p_input = THEMIM->getInput();
+    if (!p_input)
+        return;
+
+    QString openVLCBFileName = QFileDialog::getOpenFileName(
+        this, "Open VLC Bookmark File", "./",
+        "VLC Bookmark Files (*.VLCBookmark *.VLCB);;All files(*)");
+
+    if (openVLCBFileName.length() < 1)
+        return;
+    clear();
+    seekpoint_t bookmark;
+
+    QString inStr;
+    QFile file;
+
+    file.setFileName(openVLCBFileName);
+    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
+    {
+        alert("Could not open file");
+        return;
+    }
+
+    inStr = file.readAll();
+    file.close();
+
+    QJsonDocument jsonDocument = QJsonDocument::fromJson(inStr.toUtf8());
+
+    if (!jsonDocument.isObject())
+    {
+        alert("Error parsing object from file");
+        return;
+    }
+
+    QJsonObject jsonObject = jsonDocument.object();
+
+    if (!jsonObject["bookmarks"].isArray())
+    {
+        alert("Error parsing bookmarks array");
+        return;
+    }
+
+    QJsonArray bookmarkArray = jsonObject["bookmarks"].toArray();
+
+    for (int i_index = 0; i_index < bookmarkArray.size(); i_index++)
+    {
+        const QJsonValue &value = bookmarkArray[i_index];
+
+        if (!value.isObject())
+        {
+            alert("Error parsing bookmark at index: " + i_index);
+            return;
+        }
+        QJsonObject bookmarkObject = value.toObject();
+
+        QString bookmarkDescription = "";
+        vlc_tick_t seekTime = 0;
+
+        bookmarkDescription = bookmarkObject["description"].toString();
+        seekTime = bookmarkObject["time"].toInt();
+
+        QByteArray raw = bookmarkDescription.toUtf8();
+
+        bookmark.psz_name = raw.data();
+        bookmark.i_time_offset = seekTime;
+
+        input_Control(p_input, INPUT_ADD_BOOKMARK, &bookmark);
+    }
+}
+
+void BookmarksDialog::exportBookmarks()
+{
+    input_thread_t *p_input = THEMIM->getInput();
+    if (!p_input)
+        return;
+
+    QString saveVLCBFileName = QFileDialog::getSaveFileName(
+        this, "Save VLC Bookmark File",
+        "./" + THEMIM->getIM()->getName() + "_bookmarks.VLCB",
+        "VLC Bookmark Files (*.VLCBookmark *.VLCB);;All files(*)");
+
+    if (saveVLCBFileName.length() < 1)
+        return;
+
+    QFile file;
+
+    file.setFileName(saveVLCBFileName);
+    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
+    {
+        alert("Could not open file");
+        return;
+    }
+
+    QJsonArray bookmarkArray;
+
+    bookmarksList->selectAll();
+    QModelIndexList selected = bookmarksList->selectionModel()->selectedRows();
+    if (selected.empty())
+    {
+        alert("No bookmark to export!");
+        return;
+    }
+
+    seekpoint_t **pp_bookmarks;
+    seekpoint_t bookmark;
+    int i_index;
+
+    if (input_Control(p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks, &i_index) != VLC_SUCCESS)
+        return;
+
+    for (int i_index = 0; i_index < selected.size(); i_index++)
+    {
+        bookmark = (*pp_bookmarks[i_index]);
+
+        QJsonObject bookmarkObject;
+        bookmarkObject.insert("description", bookmark.psz_name);
+        bookmarkObject.insert("time", (long long)bookmark.i_time_offset);
+
+        bookmarkArray.push_back(bookmarkObject);
+    }
+
+    QJsonObject outObject;
+    outObject.insert("bookmarks", bookmarkArray);
+
+    QJsonDocument jsonDocument(outObject);
+
+    file.write(jsonDocument.toJson());
+    file.close();
 }
 
 void BookmarksDialog::activateItem( QModelIndex index )
diff --git a/modules/gui/qt/dialogs/bookmarks.hpp b/modules/gui/qt/dialogs/bookmarks.hpp
index 2551dd6..9b37cb5 100644
--- a/modules/gui/qt/dialogs/bookmarks.hpp
+++ b/modules/gui/qt/dialogs/bookmarks.hpp
@@ -43,6 +43,8 @@ private:
     QTreeWidget *bookmarksList;
     QPushButton *clearButton;
     QPushButton *delButton;
+    QPushButton *exportButton;
+    QPushButton *importButton;
     bool b_ignore_updates;
 
 private slots:
@@ -51,9 +53,11 @@ private slots:
     void del();
     void clear();
     void edit( QTreeWidgetItem *item, int column );
-    void extract();
+    void exportBookmarks();
+    void importBookmarks();
     void activateItem( QModelIndex index );
     void updateButtons();
+    void alert(QString);
 
     friend class    Singleton<BookmarksDialog>;
 };
-- 
2.7.4



More information about the vlc-devel mailing list