From 10ba5c2e7296d7e548651be70152d01f88b54911 Mon Sep 17 00:00:00 2001
From: magenoxx <magenoxx@gmail>
Date: Tue, 23 Oct 2012 17:10:56 +0400
Subject: [PATCH] Downloading images according to save_to_zip option

---
 .../mage/client/dialog/PreferencesDialog.form |  8 ++--
 .../mage/client/dialog/PreferencesDialog.java | 38 ++++++++++++++-----
 .../plugins/card/images/DownloadPictures.java |  3 ++
 .../mage/plugins/card/images/ImageCache.java  | 24 +++++++++---
 .../plugins/card/utils/CardImageUtils.java    | 22 ++++++++++-
 5 files changed, 73 insertions(+), 22 deletions(-)

diff --git a/Mage.Client/src/main/java/mage/client/dialog/PreferencesDialog.form b/Mage.Client/src/main/java/mage/client/dialog/PreferencesDialog.form
index 288eeb099b..9d4f148345 100644
--- a/Mage.Client/src/main/java/mage/client/dialog/PreferencesDialog.form
+++ b/Mage.Client/src/main/java/mage/client/dialog/PreferencesDialog.form
@@ -483,7 +483,7 @@
                               </Group>
                               <Group type="102" alignment="0" attributes="0">
                                   <EmptySpace max="-2" attributes="0"/>
-                                  <Component id="checkForNewImages1" min="-2" max="-2" attributes="0"/>
+                                  <Component id="saveToZipFiles" min="-2" max="-2" attributes="0"/>
                               </Group>
                           </Group>
                           <EmptySpace max="32767" attributes="0"/>
@@ -503,7 +503,7 @@
                           <EmptySpace type="unrelated" max="-2" attributes="0"/>
                           <Component id="checkForNewImages" min="-2" max="-2" attributes="0"/>
                           <EmptySpace max="-2" attributes="0"/>
-                          <Component id="checkForNewImages1" min="-2" max="-2" attributes="0"/>
+                          <Component id="saveToZipFiles" min="-2" max="-2" attributes="0"/>
                           <EmptySpace pref="22" max="32767" attributes="0"/>
                       </Group>
                   </Group>
@@ -536,12 +536,12 @@
                     <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="checkForNewImagesActionPerformed"/>
                   </Events>
                 </Component>
-                <Component class="javax.swing.JCheckBox" name="checkForNewImages1">
+                <Component class="javax.swing.JCheckBox" name="saveToZipFiles">
                   <Properties>
                     <Property name="text" type="java.lang.String" value="store in zip files"/>
                   </Properties>
                   <Events>
-                    <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="checkForNewImages1ActionPerformed"/>
+                    <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="saveToZipFilesActionPerformed"/>
                   </Events>
                 </Component>
               </SubComponents>
diff --git a/Mage.Client/src/main/java/mage/client/dialog/PreferencesDialog.java b/Mage.Client/src/main/java/mage/client/dialog/PreferencesDialog.java
index 7ada83c12d..4bbe9667e4 100644
--- a/Mage.Client/src/main/java/mage/client/dialog/PreferencesDialog.java
+++ b/Mage.Client/src/main/java/mage/client/dialog/PreferencesDialog.java
@@ -69,6 +69,7 @@ public class PreferencesDialog extends javax.swing.JDialog {
     public static final String KEY_CARD_IMAGES_USE_DEFAULT = "cardImagesUseDefault";
     public static final String KEY_CARD_IMAGES_PATH = "cardImagesPath";
     public static final String KEY_CARD_IMAGES_CHECK = "cardImagesCheck";
+    public static final String KEY_CARD_IMAGES_SAVE_TO_ZIP = "cardImagesSaveToZip";
     public static final String KEY_BIG_CARD_TOGGLED = "bigCardToggled";
 
     public static final String KEY_PROXY_ADDRESS = "proxyAddress";
@@ -173,7 +174,7 @@ public class PreferencesDialog extends javax.swing.JDialog {
         imageFolderPath = new javax.swing.JTextField();
         browseButton = new javax.swing.JButton();
         checkForNewImages = new javax.swing.JCheckBox();
-        checkForNewImages1 = new javax.swing.JCheckBox();
+        saveToZipFiles = new javax.swing.JCheckBox();
         jPanel6 = new javax.swing.JPanel();
         lblProxyType = new javax.swing.JLabel();
         cbProxyType = new javax.swing.JComboBox();
@@ -451,10 +452,10 @@ public class PreferencesDialog extends javax.swing.JDialog {
             }
         });
 
-        checkForNewImages1.setText("store in zip files");
-        checkForNewImages1.addActionListener(new java.awt.event.ActionListener() {
+        saveToZipFiles.setText("store in zip files");
+        saveToZipFiles.addActionListener(new java.awt.event.ActionListener() {
             public void actionPerformed(java.awt.event.ActionEvent evt) {
-                checkForNewImages1ActionPerformed(evt);
+                saveToZipFilesActionPerformed(evt);
             }
         });
 
@@ -477,7 +478,7 @@ public class PreferencesDialog extends javax.swing.JDialog {
                         .addComponent(checkForNewImages))
                     .addGroup(jPanel5Layout.createSequentialGroup()
                         .addContainerGap()
-                        .addComponent(checkForNewImages1)))
+                        .addComponent(saveToZipFiles)))
                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
         );
         jPanel5Layout.setVerticalGroup(
@@ -492,7 +493,7 @@ public class PreferencesDialog extends javax.swing.JDialog {
                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                 .addComponent(checkForNewImages)
                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
-                .addComponent(checkForNewImages1)
+                .addComponent(saveToZipFiles)
                 .addContainerGap(22, Short.MAX_VALUE))
         );
 
@@ -981,6 +982,7 @@ public class PreferencesDialog extends javax.swing.JDialog {
 
         // images
         saveImagesPath(prefs);
+        saveSaveToZipOption(prefs);
 
         try {
             prefs.flush();
@@ -1048,9 +1050,9 @@ public class PreferencesDialog extends javax.swing.JDialog {
     private void rememberPswdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rememberPswdActionPerformed
     }//GEN-LAST:event_rememberPswdActionPerformed
 
-    private void checkForNewImages1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkForNewImages1ActionPerformed
+    private void saveToZipFilesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveToZipFilesActionPerformed
         // TODO add your handling code here:
-    }//GEN-LAST:event_checkForNewImages1ActionPerformed
+    }//GEN-LAST:event_saveToZipFilesActionPerformed
 
     private void checkForNewImagesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkForNewImagesActionPerformed
         // TODO add your handling code here:
@@ -1101,6 +1103,7 @@ public class PreferencesDialog extends javax.swing.JDialog {
 
                     // Images
                     loadImagesPath(prefs);
+                    loadSaveToZip(prefs);
 
                     // open specified tab before displaying
                     openTab(openedTab);
@@ -1193,7 +1196,13 @@ public class PreferencesDialog extends javax.swing.JDialog {
         updateCache(KEY_CARD_IMAGES_CHECK, Boolean.toString(dialog.checkForNewImages.isSelected()));
     }
 
-     private static void saveImagesPath(Preferences prefs) {
+    private static void loadSaveToZip(Preferences prefs) {
+        String prop = prefs.get(KEY_CARD_IMAGES_SAVE_TO_ZIP, "false");
+        dialog.checkForNewImages.setSelected(prop.equals("true"));
+        updateCache(KEY_CARD_IMAGES_SAVE_TO_ZIP, Boolean.toString(dialog.saveToZipFiles.isSelected()));
+    }
+
+    private static void saveImagesPath(Preferences prefs) {
         if (dialog.useDefaultImageFolder.isSelected()) {
             prefs.put(KEY_CARD_IMAGES_USE_DEFAULT, "true");
             updateCache(KEY_CARD_IMAGES_USE_DEFAULT, "true");
@@ -1208,6 +1217,15 @@ public class PreferencesDialog extends javax.swing.JDialog {
         updateCache(KEY_CARD_IMAGES_CHECK, Boolean.toString(dialog.checkForNewImages.isSelected()));
     }
 
+    private static void saveSaveToZipOption(Preferences prefs) {
+        prefs.put(KEY_CARD_IMAGES_SAVE_TO_ZIP, Boolean.toString(dialog.saveToZipFiles.isSelected()));
+        updateCache(KEY_CARD_IMAGES_SAVE_TO_ZIP, Boolean.toString(dialog.saveToZipFiles.isSelected()));
+    }
+
+    public static boolean isSaveImagesToZip() {
+        return PreferencesDialog.getCachedValue(PreferencesDialog.KEY_CARD_IMAGES_SAVE_TO_ZIP, "false").equals("true");
+    }
+
     private static void load(Preferences prefs, JCheckBox checkBox, String propName, String yesValue) {
         String prop = prefs.get(propName, yesValue);
         checkBox.setSelected(prop.equals(yesValue));
@@ -1367,7 +1385,6 @@ public class PreferencesDialog extends javax.swing.JDialog {
     private javax.swing.JCheckBox checkBoxUpkeepOthers;
     private javax.swing.JCheckBox checkBoxUpkeepYou;
     private javax.swing.JCheckBox checkForNewImages;
-    private javax.swing.JCheckBox checkForNewImages1;
     private javax.swing.JCheckBox displayBigCardsInHand;
     private javax.swing.JButton exitButton;
     private javax.swing.JTextField imageFolderPath;
@@ -1417,6 +1434,7 @@ public class PreferencesDialog extends javax.swing.JDialog {
     private javax.swing.JPanel pnlProxySettings;
     private javax.swing.JCheckBox rememberPswd;
     private javax.swing.JButton saveButton;
+    private javax.swing.JCheckBox saveToZipFiles;
     private javax.swing.JCheckBox showToolTipsInAnyZone;
     private javax.swing.JPasswordField txtPasswordField;
     private javax.swing.JTextField txtProxyPort;
diff --git a/Mage.Client/src/main/java/org/mage/plugins/card/images/DownloadPictures.java b/Mage.Client/src/main/java/org/mage/plugins/card/images/DownloadPictures.java
index a306997006..1c38b658ea 100644
--- a/Mage.Client/src/main/java/org/mage/plugins/card/images/DownloadPictures.java
+++ b/Mage.Client/src/main/java/org/mage/plugins/card/images/DownloadPictures.java
@@ -457,6 +457,9 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
                 File temporaryFile = new File(Constants.IO.imageBaseDir + File.separator + card.hashCode() + "." + card.getName() + ".jpg");
                 String imagePath = CardImageUtils.getImagePath(card, imagesPath);
                 TFile outputFile = new TFile(imagePath);
+                if (!outputFile.exists()) {
+                    outputFile.getParentFile().mkdirs();
+                }
                 File existingFile = new File(imagePath.replaceFirst("\\w{3}.zip", ""));
                 if (existingFile.exists()) {
                     new TFile(existingFile).cp_rp(outputFile);
diff --git a/Mage.Client/src/main/java/org/mage/plugins/card/images/ImageCache.java b/Mage.Client/src/main/java/org/mage/plugins/card/images/ImageCache.java
index f390c4d0e1..6e54c58328 100644
--- a/Mage.Client/src/main/java/org/mage/plugins/card/images/ImageCache.java
+++ b/Mage.Client/src/main/java/org/mage/plugins/card/images/ImageCache.java
@@ -7,16 +7,18 @@ import com.mortennobel.imagescaling.ResampleOp;
 import de.schlichtherle.truezip.file.TFile;
 import de.schlichtherle.truezip.file.TFileInputStream;
 import de.schlichtherle.truezip.file.TFileOutputStream;
+import mage.client.dialog.PreferencesDialog;
+import mage.view.CardView;
+import org.apache.log4j.Logger;
+import org.mage.plugins.card.constants.Constants;
+import org.mage.plugins.card.utils.CardImageUtils;
+
+import javax.imageio.ImageIO;
 import java.awt.*;
 import java.awt.image.BufferedImage;
 import java.util.Map;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
-import javax.imageio.ImageIO;
-import mage.view.CardView;
-import org.apache.log4j.Logger;
-import org.mage.plugins.card.constants.Constants;
-import org.mage.plugins.card.utils.CardImageUtils;
 
 /**
  * This class stores ALL card images in a cache with soft values. this means
@@ -76,7 +78,7 @@ public class ImageCache {
                         TFile file = new TFile(path);
 
                         if (thumbnail && path.endsWith(".jpg")) {
-                            String thumbnailPath = path.replace(".zip", ".thumb.zip");
+                            String thumbnailPath = buildThumbnailPath(path);
                             TFile thumbnailFile = new TFile(thumbnailPath);
                             if (thumbnailFile.exists()) {
                                 //log.debug("loading thumbnail for " + key + ", path="+thumbnailPath);
@@ -107,6 +109,16 @@ public class ImageCache {
             }
         });
     }
+    
+    private static String buildThumbnailPath(String path) {
+        String thumbnailPath;
+        if (PreferencesDialog.isSaveImagesToZip()) {
+            thumbnailPath = path.replace(".zip", ".thumb.zip");
+        } else {
+            thumbnailPath = path.replace(".jpg", ".thumb.jpg");
+        }
+        return thumbnailPath;
+    }
 
     public static BufferedImage getWizardsCard(BufferedImage image) {
         if (image.getWidth() == 265 && image.getHeight() == 370) {
diff --git a/Mage.Client/src/main/java/org/mage/plugins/card/utils/CardImageUtils.java b/Mage.Client/src/main/java/org/mage/plugins/card/utils/CardImageUtils.java
index a1d48d6d0b..ac5071c197 100644
--- a/Mage.Client/src/main/java/org/mage/plugins/card/utils/CardImageUtils.java
+++ b/Mage.Client/src/main/java/org/mage/plugins/card/utils/CardImageUtils.java
@@ -1,12 +1,14 @@
 package org.mage.plugins.card.utils;
 
 import de.schlichtherle.truezip.file.TFile;
-import java.util.HashMap;
-import java.util.regex.Pattern;
+import mage.client.dialog.PreferencesDialog;
 import org.mage.plugins.card.constants.Constants;
 import org.mage.plugins.card.images.CardInfo;
 import org.mage.plugins.card.properties.SettingsManager;
 
+import java.util.HashMap;
+import java.util.regex.Pattern;
+
 public class CardImageUtils {
 
     private static final Pattern basicLandPattern = Pattern.compile("^(Forest|Mountain|Swamp|Island|Plains)$");
@@ -104,9 +106,25 @@ public class CardImageUtils {
         String set = updateSet(card.getSet(), false).toUpperCase();
         String imagesDir = (imagesPath != null ? imagesPath :  Constants.IO.imageBaseDir);
         if (card.isToken()) {
+            return buildTokenPath(imagesDir, set);
+        } else {
+            return buildPath(imagesDir, set);
+        }
+    }
+    
+    private static String buildTokenPath(String imagesDir, String set) {
+        if (PreferencesDialog.isSaveImagesToZip()) {
             return imagesDir + TFile.separator + "TOK" + ".zip" + TFile.separator + set;
         } else {
+            return imagesDir + TFile.separator + "TOK" + TFile.separator + set;
+        }
+    }
+
+    private static String buildPath(String imagesDir, String set) {
+        if (PreferencesDialog.isSaveImagesToZip()) {
             return imagesDir + TFile.separator + set + ".zip" + TFile.separator + set;
+        } else {
+            return imagesDir + TFile.separator + set;
         }
     }