Move video thumbnails to VideoThumbnails/, fix video renaming not renaming the thumbnail as well
authorWuzzy <Wuzzy2@mail.ru>
Mon, 06 May 2019 00:43:27 +0200
changeset 14887 a414d37278df
parent 14886 3054b85963ef
child 14888 b5a9dd36c7ea
Move video thumbnails to VideoThumbnails/, fix video renaming not renaming the thumbnail as well
ChangeLog.txt
QTfrontend/main.cpp
QTfrontend/ui/page/pagevideos.cpp
hedgewars/uVideoRec.pas
--- a/ChangeLog.txt	Sun May 05 17:23:59 2019 +0200
+++ b/ChangeLog.txt	Mon May 06 00:43:27 2019 +0200
@@ -93,6 +93,8 @@
  + Add button in main menu at top left corner to open credits page
  + Restructure credits page
  + More intelligent automatic mission selection in campaign screen
+ + New data directory for video thumbnails: Data/VideoThumbnails
+ * Fix renaming a video leading to loss of thumbnail after restart
  * Fix controls list failing to display correct key names with regards to keyboard layout
  * Fix force-locked schemes getting unlocked when changing map types
  * Fix possible to select background-only or hidden themes indirectly by changing map type
--- a/QTfrontend/main.cpp	Sun May 05 17:23:59 2019 +0200
+++ b/QTfrontend/main.cpp	Mon May 06 00:43:27 2019 +0200
@@ -360,6 +360,7 @@
         checkForDir(cfgdir->absolutePath() + "/Logs");
         checkForDir(cfgdir->absolutePath() + "/Videos");
         checkForDir(cfgdir->absolutePath() + "/VideoTemp");
+        checkForDir(cfgdir->absolutePath() + "/VideoThumbnails");
     }
 
     datadir->cd(bindir->absolutePath());
--- a/QTfrontend/ui/page/pagevideos.cpp	Sun May 05 17:23:59 2019 +0200
+++ b/QTfrontend/ui/page/pagevideos.cpp	Mon May 06 00:43:27 2019 +0200
@@ -218,6 +218,8 @@
 {
     nameChangedFromCode = false;
     numRecorders = 0;
+    // Clear VideoTemp at launch in case some garbage remained in here after a crash
+    clearTemp();
     initPage();
 }
 
@@ -399,17 +401,34 @@
     // user has edited filename, so we should rename the file
     VideoItem * item = nameItem(row);
     QString oldName = item->name;
+    int pointPos = oldName.lastIndexOf('.');
+    QString oldPrefix = item->name;
+    oldPrefix.truncate(pointPos);
     QString newName = item->text();
     if (!newName.contains('.')) // user forgot an extension
     {
         // restore old extension
-        int pt = oldName.lastIndexOf('.');
-        if (pt != -1)
+        pointPos = oldName.lastIndexOf('.');
+        if (pointPos != -1)
         {
-            newName += oldName.right(oldName.length() - pt);
+            newName += oldName.right(oldName.length() - pointPos);
             setName(item, newName);
         }
     }
+    QString newPrefix;
+    if (newName.contains('.'))
+    {
+        pointPos = newName.lastIndexOf('.');
+        if (pointPos != -1)
+        {
+            newPrefix = newName;
+            newPrefix.truncate(pointPos);
+        }
+    }
+    else
+    {
+        newPrefix = newName;
+    }
 #ifdef Q_OS_WIN
     // there is a bug in qt, QDir::rename() doesn't fail on such names but damages files
     if (newName.contains(QRegExp("[\"*:<>?\\/|]")))
@@ -425,7 +444,13 @@
         setName(item, oldName);
         return;
     }
+    if (item->ready())
+    {
+        cfgdir->rename("VideoThumbnails/" + oldPrefix + ".png", "VideoThumbnails/" + newPrefix + ".png");
+        cfgdir->rename("VideoThumbnails/" + oldPrefix + ".bmp", "VideoThumbnails/" + newPrefix + ".bmp");
+    }
     item->name = newName;
+    item->prefix = newPrefix;
     updateDescription();
 }
 
@@ -539,7 +564,7 @@
 
     if (!item->prefix.isEmpty())
     {
-        QString thumbName = cfgdir->absoluteFilePath("VideoTemp/" + item->prefix);
+        QString thumbName = cfgdir->absoluteFilePath("VideoThumbnails/" + item->prefix);
         QPixmap pic;
         if (pic.load(thumbName + ".png") || pic.load(thumbName + ".bmp"))
         {
@@ -604,8 +629,8 @@
     {
         cfgdir->remove("Videos/" + item->name);
         // we have no idea whether screenshot is going to be bmp or png so let's delete both
-        cfgdir->remove("VideoTemp/" + item->prefix + ".png");
-        cfgdir->remove("VideoTemp/" + item->prefix + ".bmp");
+        cfgdir->remove("VideoThumbnails/" + item->prefix + ".png");
+        cfgdir->remove("VideoThumbnails/" + item->prefix + ".bmp");
     }
 
 // this code is for removing several files when multiple selection is enabled
@@ -664,14 +689,21 @@
     QDesktopServices::openUrl(QUrl("file:///" + path));
 }
 
-// clear VideoTemp directory (except for thumbnails)
+// clear VideoTemp directory
 void PageVideos::clearTemp()
 {
+    qDebug("Clearing VideoTemp directory ...");
     QDir temp(cfgdir->absolutePath() + "/VideoTemp");
     QStringList files = temp.entryList(QDir::Files);
     foreach (const QString& file, files)
     {
-        if (!file.endsWith(".bmp") && !file.endsWith(".png"))
+        // Legacy support: Move thumbnails to correct dir
+        if (file.endsWith(".bmp") || file.endsWith(".png"))
+        {
+            qDebug("Moving video thumbnail '%s' to VideoThumbnails directory", qPrintable(file));
+            cfgdir->rename("VideoTemp/" + file, "VideoThumbnails/" + file);
+        }
+        else
             temp.remove(file);
     }
 }
--- a/hedgewars/uVideoRec.pas	Sun May 05 17:23:59 2019 +0200
+++ b/hedgewars/uVideoRec.pas	Mon May 06 00:43:27 2019 +0200
@@ -227,7 +227,7 @@
 var thumbpath: shortstring;
     k: LongInt;
 begin
-    thumbpath:= '/VideoTemp/' + RecPrefix;
+    thumbpath:= '/VideoThumbnails/' + RecPrefix;
     AddFileLog('Saving thumbnail ' + thumbpath);
     k:= max(max(cScreenWidth, cScreenHeight) div 400, 1); // here 400 is minimum size of thumbnail
     MakeScreenshot(thumbpath, k, 0);