Browse Source

filechooser mode added based on a special magic path

LH 2 years ago
parent
commit
7c1b3a974d

+ 51 - 28
iZpl/src/main/java/de/nplusc/izc/iZpl/Main.java

@@ -348,6 +348,10 @@ public class Main extends javax.swing.JFrame
             try
             {
                 CONFIG = (Configuration) y.load(new FileReader(CONFIGPATH));
+                if(CONFIG.getCustomProperties()==null)
+                {
+                    CONFIG.setCustomProperties(new HashMap<>());
+                }
             }
             catch (FileNotFoundException ex)
             {
@@ -360,6 +364,7 @@ public class Main extends javax.swing.JFrame
             new File(CONFIGPATH).getParentFile().mkdirs();
             CONFIG.setMPExecutablePath("");
             CONFIG.setSkinSelected("default");
+            CONFIG.setCustomProperties(new HashMap<>());
             CONFIG.setMediaPlayerForStandalone("VLC Embedded");
             CONFIG.setUsedPluginUI("IZPL-GUI Skinnable");
             CONFIG.setSelectedMode(Configuration.UIMode.INCLUDED);
@@ -647,10 +652,8 @@ public class Main extends javax.swing.JFrame
     }
 
 
-    
-    private static void reload() throws FileNotFoundException, IOException
-    {
-        String tpath = stts.file_path;
+    static PlProcessorV2 reloadFromFile(String tpath) throws IOException {
+
         boolean sym = true;
         int redirs = 0;
         int max_redirs = 10;//Loopstopper!!!
@@ -664,6 +667,9 @@ public class Main extends javax.swing.JFrame
             }
             FileReader plf = new FileReader(tpath);
             char[] magix = new char[3];
+            //V--nothing to see here
+
+            //^--evil line doesnt need any code :P
             plf.read(magix);
             if (new String(magix).equalsIgnoreCase("SYM"))
             {
@@ -685,29 +691,29 @@ public class Main extends javax.swing.JFrame
             pp = (PlProcessorV2) u;
             //HACK, encodingissue rausficken
             BiConsumer<PlayListItem,BiConsumer> haxxz0r = (e,f)->
+            {
+                if(e instanceof SinglePlayListItem)
+                {
+                    SinglePlayListItem itm = ((SinglePlayListItem) e);
+                    String dbg1 = Base64.getEncoder().encodeToString(itm.getPath().getBytes());
+                    String dbg2 = Base64.getEncoder().encodeToString(itm.getTitle().getBytes());
+                    itm.setDebug(dbg1+"|||||"+dbg2);
+                }
+                else
+                {
+                    if(e instanceof MultiPlayListItem)
                     {
-                        if(e instanceof SinglePlayListItem)
+                        try
                         {
-                            SinglePlayListItem itm = ((SinglePlayListItem) e);
-                            String dbg1 = Base64.getEncoder().encodeToString(itm.getPath().getBytes());
-                            String dbg2 = Base64.getEncoder().encodeToString(itm.getTitle().getBytes());
-                            itm.setDebug(dbg1+"|||||"+dbg2);
+                            ((MultiPlayListItem) e).getItems().forEach((x)->f.accept(e, f));
                         }
-                        else
+                        catch(StackOverflowError ignored)
                         {
-                            if(e instanceof MultiPlayListItem)
-                            {
-                                try
-                                {
-                                    ((MultiPlayListItem) e).getItems().forEach((x)->f.accept(e, f));
-                                }
-                                catch(StackOverflowError ignored)
-                                {
-                                    //do nothing, guards against a unexpected recursion that went too deep.
-                                }
-                            }
+                            //do nothing, guards against a unexpected recursion that went too deep.
                         }
-                    };
+                    }
+                }
+            };
             pp.getPool().forEach((a,p)->
             {
                 haxxz0r.accept(p, haxxz0r);
@@ -719,15 +725,37 @@ public class Main extends javax.swing.JFrame
             pp.InitializeOnPath(((PlProcessor) u).getPath());
         }
         plf.close();
+        return pp;
+    }
+    
+    private static void reload() throws FileNotFoundException, IOException
+    {
+        String tpath = stts.file_path;
+        PlProcessorV2 pp = reloadFromFile(tpath);
         l.trace("usegui=" + stts.useGUI);
         ps = new PLServer(pp, !stts.useGUI);
     }
+
+    static void saveConfig()
+    {
+        try
+        {
+            y.dump(CONFIG, new FileWriter(CONFIGPATH));
+        }
+        catch (IOException ex)
+        {
+            ex.printStackTrace();
+        }
+    }
+
     /**
      * Triggers a save of the state then exiting the program
      * @param isShittyPlayer parameter that tells if the Program detected a player incompatible with the pregeneration mode
      */
     public static void checkpointedExit(boolean isShittyPlayer)
     {
+
+
         l.trace("<<<");
         String time = new Date(System.currentTimeMillis()).toString();
         l.info("IZPL-Core:Checkpoint erstellen:" + time);
@@ -756,12 +784,7 @@ public class Main extends javax.swing.JFrame
         }
         quickQuit();
     }
-    
-    
-    
-    //V--nothing to see here
-    
-    //^--evil line doesnt need any code :P
+
     
     /**
      * Convenience method that cleans up the temporary data and then exits

+ 151 - 5
iZpl/src/main/java/de/nplusc/izc/iZpl/PlProcessorV2.java

@@ -23,6 +23,7 @@ import com.sun.net.httpserver.HttpExchange;
 import com.sun.net.httpserver.HttpHandler;
 import de.nplusc.izc.iZpl.API.IZPLApi;
 import de.nplusc.izc.iZpl.API.shared.InvalidPlayListFileException;
+import de.nplusc.izc.iZpl.API.shared.SinglePlayListItem;
 import de.nplusc.izc.iZpl.Utils.shared.PLFileIO;
 import java.io.File;
 import java.io.IOException;
@@ -38,6 +39,8 @@ import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Set;
+
+import de.nplusc.izc.tools.baseTools.Detectors;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.yaml.snakeyaml.Yaml;
@@ -48,6 +51,8 @@ import org.yaml.snakeyaml.Yaml;
  */
 public class PlProcessorV2 implements HttpHandler 
 {
+    private boolean FileChooserMode = false;
+    private boolean winpath = false;
     private static final Logger l = LogManager.getLogger();
     private HashMap<String,PlayListItem> pool;
     private String path;
@@ -152,6 +157,37 @@ public class PlProcessorV2 implements HttpHandler
     
     public void InitializeOnPath(String PlPath)
     {
+        if(PlPath.equals("///")) //abusing a invalid path for
+        {
+            FileChooserMode = true;
+            if(Detectors.getSystemClassification()[0].equals("windows"))
+            {
+                winpath = true; //used only for handling filechooser logic
+            }
+            String lastchooserpath = Main.CONFIG.getProperty("lastchooserpath");
+            if(lastchooserpath == null)
+            {
+                lastchooserpath = System.getProperty("user.home");
+                File tmp = new File(lastchooserpath);
+                if(!(tmp.exists()&&tmp.isDirectory()))
+                {
+                    lastchooserpath=winpath?"C:/":"/";
+                }
+            } else
+            {
+                File tmp = new File(lastchooserpath);
+                if(!(tmp.exists()&&tmp.isDirectory()))
+                {
+                    lastchooserpath = System.getProperty("user.home");
+                    tmp = new File(lastchooserpath);
+                    if(!(tmp.exists()&&tmp.isDirectory()))
+                    {
+                        lastchooserpath=winpath?"C:/":"/";
+                    }
+                }
+            }
+            PlPath = lastchooserpath;
+        }
         Thread t = new Thread(this::FSWatch);
         t.setName("FS-Observer");
         t.setDaemon(true);
@@ -166,7 +202,71 @@ public class PlProcessorV2 implements HttpHandler
     {
         reloadLists(false);
     }
-    
+
+    private boolean fileIsRoot(File f)
+    {
+        for(File iter:File.listRoots())
+        {
+            if(iter.equals(f))
+            {
+                return true;
+            }
+        }
+        return false;
+    }
+
+
+    List<PlayListItem> synthesizeChooserData(String path)
+    {
+        Main.CONFIG.setProperty("lastchooserpath",path);
+        List<PlayListItem> lst = new ArrayList<>();
+        if(winpath&&path.equals("/")) //simulating root dir for showing drive letters
+        {
+            for(File iter:File.listRoots())
+            {
+                SinglePlayListItem itm = new SinglePlayListItem(iter.getAbsolutePath(),"0,"+iter.getAbsolutePath(),1);
+                lst.add(itm);
+            }
+        }
+        else
+        {
+            File f = new File(path).getAbsoluteFile();
+            if(winpath&&fileIsRoot(f))
+            {
+                SinglePlayListItem itm = new SinglePlayListItem("/","0,"+"../",1);
+                lst.add(itm);
+            }
+            else
+            {
+                if(!(path.equals("/"))) //no .. entry on rootdir
+                {
+                    SinglePlayListItem itm = new SinglePlayListItem(f.getParent(),"0,"+"../",1);
+                    lst.add(itm);
+                }
+            }
+            for(File iter: f.listFiles())
+            {
+                String fname = iter.getName();
+                String[] fextinternal = fname.split("\\.");
+                String fext = fextinternal[fextinternal.length-1];
+
+                if(iter.isDirectory())
+                {
+                    SinglePlayListItem itm = new SinglePlayListItem(iter.getAbsolutePath(),"0,"+fname+"/",1);
+                    lst.add(itm);
+
+                }
+                else if(fext.equalsIgnoreCase("izpl")||fext.equalsIgnoreCase("izcont")||fext.equalsIgnoreCase("m3u"))
+                {
+                    SinglePlayListItem itm = new SinglePlayListItem(iter.getAbsolutePath(),"0,"+fname,1);
+                    lst.add(itm);
+                }
+            }
+        }
+        l.trace(yp.dump(lst));
+        return lst;
+    }
+
     public void reloadLists(boolean reload)
     { 
 
@@ -175,15 +275,51 @@ public class PlProcessorV2 implements HttpHandler
             List<PlayListItem> inp;
             try
             {
-                inp = PLFileIO.parseFullList(path,this::registerChangedFolders);
+                if(FileChooserMode)
+                {
+                    if((new File(path).isDirectory()||path.equals("/")))
+                    {
+                        inp = synthesizeChooserData(path);
+                    }
+                    else
+                    {
+                        String[] fextinternal = new File(path).getName().split("\\.");
+                        String fext = fextinternal[fextinternal.length-1];
+                        if(fext.equalsIgnoreCase("izcont"))
+                        {
+                            FileChooserMode = false;
+                            PlProcessorV2 copysrc = Main.reloadFromFile(path);
+                            this.options = copysrc.options;
+                            this.pool = copysrc.pool;
+                            this.forceList = copysrc.forceList;
+                            this.history = copysrc.history;
+                            this.path = copysrc.path;
+                            inp = PLFileIO.parseFullList(path,this::registerChangedFolders);
+                        }
+                        else
+                        {
+                            FileChooserMode = false;
+                            inp = PLFileIO.parseFullList(path,this::registerChangedFolders);
+                        }
+                        Main.saveConfig();
+                    }
+                }
+                else
+                {
+                    inp = PLFileIO.parseFullList(path,this::registerChangedFolders);
+                }
             }
             catch (InvalidPlayListFileException ex)
             {
                 l.error("invalid playlist file");
                 Main.quickQuit();
                 return;
+            } catch (IOException e) {
+                l.error("invalid playlist file");
+                Main.quickQuit();
+                return;
             }
-            int clampPlayCount = 0;
+        int clampPlayCount = 0;
             if(options.containsKey("PLAYCOUNTLIMIT"))
             {
                 try
@@ -372,8 +508,18 @@ public class PlProcessorV2 implements HttpHandler
     
     public void addElementToForcedList(PlayListItem i)
     {
-        forceList.add(i);
-        IZPLApi.refreshScheduledItemList();
+        if(FileChooserMode)
+        {
+            path = i.getPath();
+            reloadLists();
+            IZPLApi.getUIPlugin().reloadPlayList();
+
+        }
+        else
+        {
+            forceList.add(i);
+            IZPLApi.refreshScheduledItemList();
+        }
     }
 
     public int getPoolSize()

+ 8 - 2
iZplPlugins/JukeBox/src/main/java/de/nplusc/izc/izpl/plugins/jukebox/JukeBox.java

@@ -6,6 +6,7 @@
 package de.nplusc.izc.izpl.plugins.jukebox;
 
 import de.nplusc.izc.iZpl.API.FeaturePlugin;
+import de.nplusc.izc.iZpl.API.IZPLApi;
 import de.nplusc.izc.iZpl.API.UIPlugin;
 import de.nplusc.izc.iZpl.API.shared.InvalidPlayListFileException;
 import de.nplusc.izc.tools.baseTools.Detectors;
@@ -77,8 +78,13 @@ public class JukeBox extends javax.swing.JFrame implements MouseListener,Playbac
         lblStatus = new javax.swing.JLabel();
         lblTrackName = new javax.swing.JLabel();
 
-        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
-
+        setDefaultCloseOperation (DO_NOTHING_ON_CLOSE);
+        addWindowListener(new java.awt.event.WindowAdapter() {
+            @Override
+            public void windowClosing(java.awt.event.WindowEvent windowEvent) {
+                IZPLApi.shutdownWithSave();
+            }
+        });
         tblPLE.setAutoCreateRowSorter(true);
         tblPLE.setModel(new javax.swing.table.DefaultTableModel(
             new Object [][]