[kde-doc-english] [kwave] /: saveblocks: allow path separators in filename patterns to make it possible to create subdirectories

Thomas Eschenbacher Thomas.Eschenbacher at gmx.de
Wed Oct 28 19:12:55 UTC 2015


Git commit f74bff5eebba29cfa574bb8a65d575979e6b8a41 by Thomas Eschenbacher.
Committed on 28/10/2015 at 18:51.
Pushed by eschenbacher into branch 'master'.

saveblocks: allow path separators in filename patterns to make it possible to create subdirectories

M  +2    -0    CHANGES
M  +8    -0    doc/en/index.docbook
M  +94   -31   plugins/saveblocks/SaveBlocksPlugin.cpp
M  +12   -0    plugins/saveblocks/SaveBlocksPlugin.h

http://commits.kde.org/kwave/f74bff5eebba29cfa574bb8a65d575979e6b8a41

diff --git a/CHANGES b/CHANGES
index 891d515..8994650 100644
--- a/CHANGES
+++ b/CHANGES
@@ -11,6 +11,8 @@
  * cmdline option "--nofork" no longer exists
  * bugfix: multiple issues in context of switching the GUI type in scripts
  * saveblocks: fixed issues with special characters in filenames and patterns
+ * saveblocks: allow path separators in filename patterns to make it possible
+   to create subdirectories
 
 0.9.0 [2015-05-25]
 
diff --git a/doc/en/index.docbook b/doc/en/index.docbook
index c2be02a..2169727 100644
--- a/doc/en/index.docbook
+++ b/doc/en/index.docbook
@@ -6915,6 +6915,14 @@
 		a pattern that can contain the original file name, an index
 		and the number of sections.
 	    </para>
+	    <para>
+		It is also allowed that the file name pattern contains forward
+		slashes as path separators, which allows saving the sections
+		into different sub directories. Please note that all whitespace
+		characters around such path separators are silently removed, to
+		avoid creation of directory names which begin or end with a
+		whitespace.
+	    </para>
 	    </listitem>
 	</varlistentry>
 	<varlistentry>
diff --git a/plugins/saveblocks/SaveBlocksPlugin.cpp b/plugins/saveblocks/SaveBlocksPlugin.cpp
index 3c34813..298896f 100644
--- a/plugins/saveblocks/SaveBlocksPlugin.cpp
+++ b/plugins/saveblocks/SaveBlocksPlugin.cpp
@@ -153,6 +153,31 @@ QStringList *Kwave::SaveBlocksPlugin::setup(QStringList &previous_params)
 }
 
 //***************************************************************************
+QString Kwave::SaveBlocksPlugin::createDisplayList(
+    const QStringList &list,
+    unsigned int max_entries) const
+{
+    if (!max_entries || list.isEmpty()) return QString();
+
+    QString retval;
+    unsigned int count = 0;
+
+    foreach (const QString &entry, list) {
+	if (count == 0) // first entry
+	    retval = _("<br><br>");
+	if (count < max_entries)
+	    retval += entry + _("<br>");
+	else if (count == max_entries)
+	    retval += i18n("...") + _("<br>");
+
+	if (++count > max_entries)
+	    break;
+    }
+
+    return retval;
+}
+
+//***************************************************************************
 int Kwave::SaveBlocksPlugin::start(QStringList &params)
 {
     qDebug("SaveBlocksPlugin::start()");
@@ -166,6 +191,7 @@ int Kwave::SaveBlocksPlugin::start(QStringList &params)
     QString path = file.absolutePath();
     QString ext  = file.suffix();
     QString base = findBase(filename, m_pattern);
+    QByteArray sep("/");
 
     // determine the selection settings
     sample_index_t selection_left  = 0;
@@ -196,51 +222,81 @@ int Kwave::SaveBlocksPlugin::start(QStringList &params)
 //     qDebug("selection_only   = %d", selection_only);
 //     qDebug("indices          = %u...%u (count=%u)", first, first+count-1,count);
 
-    // check for filenames that might be overwritten
-    const int max_overwrite_list_length = 7;
-    QDir dir(path, _("*"));
-    QStringList files;
-    files = dir.entryList();
-
-    QStringList overwritten;
+    // iterate over all blocks to check for overwritten files and missing dirs
+    QStringList  overwritten_files;
+    QStringList  missing_dirs;
     for (unsigned int i = first; i < (first + count); i++) {
 	QString name = createFileName(base, ext, m_pattern, i, count,
 	                              first + count - 1);
-	QRegExp rx(_("^(") + name + _(")$"),
-	           Qt::CaseInsensitive);
-	QStringList matches = files.filter(rx);
-	if (matches.count() > 0) {
-	    overwritten += Kwave::Parser::unescape(name);
-	    if (overwritten.count() > max_overwrite_list_length)
-		break; // we have collected enough names...
+	QString display_name = Kwave::Parser::unescape(name);
+
+	// split the name into directory and file name
+	name = QString::fromLatin1(QUrl::toPercentEncoding(display_name, sep));
+	QUrl url = m_url.adjusted(QUrl::RemoveFilename);
+	url.setPath(url.path(QUrl::FullyEncoded) + name, QUrl::StrictMode);
+
+	QString filename = url.path();
+	QFileInfo file_info(filename);
+
+	// check for potentially overwritten file
+	if (file_info.exists())
+	    overwritten_files += Kwave::Parser::unescape(display_name);
+
+	// check for missing subdirectory
+	if (!file_info.dir().exists()) {
+	    QFileInfo inf(display_name);
+	    QString missing_dir = inf.path();
+	    if (!missing_dirs.contains(missing_dir))
+		missing_dirs += missing_dir;
 	}
     }
-    if (overwritten.count()) {
-	// ask the user for confirmation if he really wants to overwrite...
-
-	QString list = _("<br><br>");
-	int cnt = 0;
-	for (QStringList::Iterator it = overwritten.begin();
-	     it != overwritten.end() && (cnt <= max_overwrite_list_length);
-	     ++it, ++cnt)
-	{
-	    list += (*it);
-	    list += _("<br>");
-	}
-	if (overwritten.count() > max_overwrite_list_length)
-	    list += i18n("...");
-	list += _("<br>");
 
+    // inform about overwritten files
+    if (!overwritten_files.isEmpty()) {
+	// ask the user for confirmation if he really wants to overwrite...
 	if (Kwave::MessageBox::warningYesNo(parentWidget(),
 	    _("<html>") +
 	    i18n("This would overwrite the following file(s): %1" \
 	    "Do you really want to continue?",
-	    list) + _("</html>")) != KMessageBox::Yes)
+	    createDisplayList(overwritten_files, 5)) +
+	    _("</html>") ) != KMessageBox::Yes)
 	{
 	    return -1;
 	}
     }
 
+    // handle missing directories
+    if (!missing_dirs.isEmpty()) {
+	// ask the user if he wants to continue and create the directory
+	if (Kwave::MessageBox::warningContinueCancel(parentWidget(),
+	    i18n("The following directories do not exist: %1"
+	         "Do you want to create them and continue?",
+	         createDisplayList(missing_dirs, 5)),
+	    QString(),
+	    QString(),
+	    QString(),
+	    _("saveblocks_create_missing_dirs")
+	    ) != KMessageBox::Continue)
+	{
+	    return -1;
+	}
+
+	// create all missing directories
+	QUrl base_url = m_url.adjusted(QUrl::RemoveFilename);
+	foreach (const QString &missing, missing_dirs) {
+	    QUrl url(base_url);
+	    url.setPath(
+		base_url.path(QUrl::FullyEncoded) +
+		QString::fromLatin1(QUrl::toPercentEncoding(missing)),
+		QUrl::StrictMode
+	    );
+	    QString path = url.path();
+	    QDir dir;
+	    if (!dir.mkpath(path))
+		qWarning("creating path '%s' failed", DBG(path));
+	}
+    }
+
     // save the current selection, we have to restore it afterwards!
     sample_index_t saved_selection_left  = 0;
     sample_index_t saved_selection_right = 0;
@@ -276,7 +332,7 @@ int Kwave::SaveBlocksPlugin::start(QStringList &params)
                                           first + count - 1);
 	    name = Kwave::Parser::unescape(name);
 	    // use URL encoding for the filename
-	    name = QString::fromLatin1(QUrl::toPercentEncoding(name));
+	    name = QString::fromLatin1(QUrl::toPercentEncoding(name, sep));
 	    QUrl url = m_url.adjusted(QUrl::RemoveFilename);
 	    url.setPath(url.path(QUrl::FullyEncoded) + name, QUrl::StrictMode);
 
@@ -416,6 +472,13 @@ QString Kwave::SaveBlocksPlugin::createFileName(const QString &base,
     }
 
     if (ext.length()) p += _(".") + ext;
+
+    // sanitize the filename/path, make sure that there are no spaces
+    // before and after all path separators
+    QString sep = _("/");
+    QRegExp rx_sep(_("\\s*") + sep + _("\\s*"));
+    p.replace(rx_sep, sep);
+
     return p;
 }
 
diff --git a/plugins/saveblocks/SaveBlocksPlugin.h b/plugins/saveblocks/SaveBlocksPlugin.h
index 0c4836f..a7fc25d 100644
--- a/plugins/saveblocks/SaveBlocksPlugin.h
+++ b/plugins/saveblocks/SaveBlocksPlugin.h
@@ -154,6 +154,18 @@ namespace Kwave
 
     private:
 
+	/**
+	 * internal helper to create a string that contains a HTML
+	 * formated list of file names or directories
+	 * @param list a list of file names or directories
+	 * @param max_entries maximum number of entries to render
+	 * @return the list as a single string, separated by "\<br\>"
+	 */
+	QString createDisplayList(const QStringList &list,
+                                  unsigned int max_entries) const;
+
+    private:
+
 	/** the URL of the first file (user selection) */
 	QUrl m_url;
 



More information about the kde-doc-english mailing list