1 /*
2  * Diese Datei ist Teil des GDialog Projektes:
3  * "Gigaset-Erweiterung f�r ProjectX"
4  *
5  * Das GDialog Projekt ist freigegeben unter
6  * der GNU Public Licence (GPL), deren Text sich in
7  * dem Quellen-Verzeichnis befindet. Ist er dort nicht
8  * mehr vorhanden, so kann er unter http://www.gnu.org/licenses/gpl.html
9  * eingesehen werden.
10  */
11 package de.m740.projectx.gigaset;
12 
13 import java.awt.BorderLayout;
14 import java.awt.EventQueue;
15 import java.awt.GridLayout;
16 import java.awt.Toolkit;
17 import java.awt.event.ActionEvent;
18 import java.awt.event.ActionListener;
19 import java.awt.event.MouseAdapter;
20 import java.awt.event.MouseEvent;
21 import java.io.File;
22 import java.io.FilenameFilter;
23 import java.io.IOException;
24 import java.io.RandomAccessFile;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Comparator;
28 import java.util.HashMap;
29 import java.util.List;
30 
31 import javax.swing.JButton;
32 import javax.swing.JFileChooser;
33 import javax.swing.JOptionPane;
34 import javax.swing.JPanel;
35 import javax.swing.ListSelectionModel;
36 import javax.swing.event.ListSelectionEvent;
37 import javax.swing.event.ListSelectionListener;
38 
39 import net.sourceforge.dvb.projectx.common.Common;
40 import net.sourceforge.dvb.projectx.common.Settings;
41 
42 /**
43  * 26.01.2005
44  *
45  * @author truels
46  *
47  */
48 public class GPanelLocal extends GPanelAbstract {
49 
50     // ----------------------------------------------------------
51     class FileComparator implements Comparator {
52 
compare(Object arg0, Object arg1)53         public int compare(Object arg0, Object arg1) {
54             try {
55                 String tmpFN0 = (String) arg0;
56                 String tmpFN1 = (String) arg1;
57                 return -tmpFN0.compareTo(tmpFN1);
58             } catch (Exception e) {
59                 // ignore
60             }
61             return 0;
62         }
63 
64     }
65 
66     // ----------------------------------------------------------
67     private static final long serialVersionUID = 1L;
68 
69     private static final String PROPERTY_AUFNAHME_VERZEICHNIS = "gigaset.record.dir";
70 
71     private static final String FN_AUFNAHME_VERZEICHNIS = "gigaset-aufnahme-verzeichnis.txt";
72 
73     private static final String T = "" + File.separatorChar;
74 
75     private File flAufnahmeOrdner = new File(System.getProperty("user.dir"));
76 
77     private JButton btnAufnahmeOrdner = new JButton();
78 
79     private JButton btnRefresh = new JButton(GStrings
80             .getString("gdialog.refresh"));
81 
82     private JFileChooser fcAufnahmeOrdner = new JFileChooser(flAufnahmeOrdner);
83 
84     private FileComparator aFileComparator = null;
85 
86     /**
87      *
88      * Constructor for this class
89      *
90      * @param inpDialog
91      */
GPanelLocal(GDialog inpDialog)92     public GPanelLocal(GDialog inpDialog) {
93         super();
94         setDialog(inpDialog);
95         buildPanel();
96     }
97 
buildPanel()98     protected void buildPanel() {
99         setLayout(new BorderLayout());
100         fcAufnahmeOrdner.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
101         flAufnahmeOrdner = readAufnahmeordner();
102         String tmpString = String.valueOf(flAufnahmeOrdner);
103         btnAufnahmeOrdner.setText(shortPathOf(tmpString));
104         btnAufnahmeOrdner.setToolTipText(tmpString);
105         // sucheAufnahmen();
106 
107         // Buttons top
108         JPanel tmpPanelTop = new JPanel(new GridLayout(1, 2));
109         tmpPanelTop.add(btnAufnahmeOrdner);
110         tmpPanelTop.add(btnRefresh);
111         // Buttons oben
112         add(tmpPanelTop, BorderLayout.NORTH);
113         // Aufnahmetabelle
114         add(spAufnahmen, BorderLayout.CENTER);
115         //
116         ListSelectionModel tmpSelectionModel = aTableAufnahmen
117                 .getSelectionModel();
118         tmpSelectionModel.addListSelectionListener(new ListSelectionListener() {
119             public void valueChanged(ListSelectionEvent e) {
120                 if (!e.getValueIsAdjusting()) {
121                     doEnabled();
122                 }
123             }
124         });
125 
126         aTableAufnahmen.addMouseListener(new MouseAdapter() {
127             public void mouseClicked(MouseEvent event) {
128                 mouseClick(event);
129             }
130         });
131 
132         btnAufnahmeOrdner.addActionListener(new ActionListener() {
133             public void actionPerformed(ActionEvent e) {
134                 getFcAufnahmeOrdner()
135                         .setCurrentDirectory(getFlAufnahmeOrdner());
136                 if (getFcAufnahmeOrdner().showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
137                     getDialog().zeigeInfos(-1);
138                     setFlAufnahmeOrdner(getFcAufnahmeOrdner().getSelectedFile());
139                     String tmpPathname = String.valueOf(getFlAufnahmeOrdner());
140                     getBtnAufnahmeOrdner().setText(shortPathOf(tmpPathname));
141                     getBtnAufnahmeOrdner().setToolTipText(tmpPathname);
142                     // Common.getSettings().setProperty(PROPERTY_AUFNAHME_VERZEICHNIS,
143                     // tmpPathname);
144                     try {
145                         RandomAccessFile konfDatei = new RandomAccessFile(
146                                 FN_AUFNAHME_VERZEICHNIS, "rw");
147                         konfDatei.setLength(0);
148                         konfDatei.writeBytes(getFlAufnahmeOrdner().toString());
149                         konfDatei.close();
150                     } catch (IOException e2) {
151                     }
152                     sucheAufnahmen();
153                 }
154             }
155         });
156 
157         btnRefresh.addActionListener(new ActionListener() {
158             public void actionPerformed(ActionEvent e) {
159                 sucheAufnahmen();
160             }
161         });
162 
163     }
164 
165     /**
166      *
167      */
readAufnahmeordner()168     private File readAufnahmeordner() {
169         File tmpFile = null;
170         try {
171             Settings tmpSettings = Common.getSettings();
172             String tmpProp = PROPERTY_AUFNAHME_VERZEICHNIS;
173             String tmpFN = tmpSettings.getProperty(tmpProp);
174             if (tmpFN == null) {
175                 RandomAccessFile konfDatei = new RandomAccessFile(
176                         FN_AUFNAHME_VERZEICHNIS, "r");
177                 tmpFN = konfDatei.readLine();
178                 konfDatei.close();
179             }
180             tmpFile = new File(tmpFN);
181             if (!tmpFile.exists()) {
182                 tmpFile = new File(System.getProperty("user.dir"));
183             }
184             tmpFN = String.valueOf(tmpFile);
185             // tmpSettings.setProperty(tmpProp, tmpFN);
186         } catch (IOException e) {
187         }
188         return tmpFile;
189     }
190 
191     /**
192      *
193      * @param inpAufnahmeOrdner
194      */
sucheAufnahmen2(File inpAufnahmeOrdner)195     private void sucheAufnahmen2(File inpAufnahmeOrdner) {
196         clearSelection();
197         aTableModel.clear();
198         aTableModel.fireTableDataChanged();
199         fireStateChanged();
200         //
201         if (inpAufnahmeOrdner != null && inpAufnahmeOrdner.isDirectory()) {
202             String cridDateien[] = inpAufnahmeOrdner.list(new FilenameFilter() {
203                 public boolean accept(File f, String s) {
204                     return s.toLowerCase().endsWith(".crid");
205                 }
206             });
207             int anz = (cridDateien == null) ? 0 : cridDateien.length;
208             if (anz > 1) {
209                 Arrays.sort(cridDateien, getFileComparator());
210                 setSortSelector(SORT_FILES_NO);
211             }
212             aAufnahmen = new ArrayList(anz);
213             HashMap tmpHash = new HashMap(anz);
214             for (int i = 0; i < anz; i++) {
215                 try {
216                     String tmpFN = cridDateien[i];
217                     GAufnahmeLocal tmpAufnahme = new GAufnahmeLocal(
218                             inpAufnahmeOrdner + T + tmpFN);
219                     if (tmpAufnahme.isValid()) {
220                         aAufnahmen.add(tmpAufnahme);
221                         addAufnahme(i, tmpAufnahme, tmpHash);
222                     } else {
223                         GConsole.setMessage("Invalid file " + tmpFN
224                                 + " skipped!");
225                     }
226                 } catch (Exception exc) {
227                     GConsole.setErrorMessage(exc);
228                 }
229             } // for i
230             countChildren(GPanelTableIF.SORT_FILES_ON);
231             // evtl letzte Tree-�nderungen sichtbar machen
232             aTableModel.fireTableRowsUpdated(0, aTableModel.getRowCount() - 1);
233             tmpHash = null;
234 
235             if (aTableModel.getRowCount() == 0) {
236                 // falls keine Aufnahmen vorh.
237                 getDialog().zeigeInfos(-1); // soll auch nix angezeigt w.!
238             }
239         }
240     }
241 
242     /**
243      *
244      * @param inpAufnahmeOrdner
245      */
sucheAufnahmen1(File inpAufnahmeOrdner)246     protected void sucheAufnahmen1(File inpAufnahmeOrdner) {
247         try {
248             sucheAufnahmen2(inpAufnahmeOrdner);
249         } catch (Exception e) {
250             GConsole.setErrorMessage(e);
251         } finally {
252             btnAufnahmeOrdner.setEnabled(true);
253             btnRefresh.setEnabled(true);
254         }
255     }
256 
257     /**
258      *
259      */
getSelectedRecording()260     public List getSelectedRecording() {
261         try {
262             GAufnahmeIF tmpAufnahme = getSelectedAufnahme();
263             GConsole.setMessage("Get selected recording: "
264                     + tmpAufnahme.getTitel());
265             return tmpAufnahme.getMpgFiles();
266         } catch (RuntimeException exc) {
267             GConsole.setErrorMessage(exc);
268         }
269         return null; // error
270     }
271 
272     /**
273      *
274      */
sucheAufnahmen()275     public void sucheAufnahmen() {
276         Runnable tmpRunnable = new Runnable() {
277             public void run() {
278                 try {
279                     sucheAufnahmen1(getFlAufnahmeOrdner());
280                 } catch (Exception exc) {
281                     GConsole.setErrorMessage(exc);
282                 }
283             }
284 
285         };
286         try {
287             btnAufnahmeOrdner.setEnabled(false);
288             btnRefresh.setEnabled(false);
289             GConsole.clear();
290             if (EventQueue.isDispatchThread()) {
291                 new Thread(tmpRunnable).start();
292             } else {
293                 EventQueue.invokeLater(tmpRunnable);//
294             }
295         } catch (Exception e) {
296             GConsole.setErrorMessage(e);
297         }
298     }
299 
getTableAufnahmen()300     protected GTable getTableAufnahmen() {
301         return aTableAufnahmen;
302     }
303 
getFcAufnahmeOrdner()304     protected JFileChooser getFcAufnahmeOrdner() {
305         return fcAufnahmeOrdner;
306     }
307 
getBtnAufnahmeOrdner()308     protected JButton getBtnAufnahmeOrdner() {
309         return btnAufnahmeOrdner;
310     }
311 
getFlAufnahmeOrdner()312     public File getFlAufnahmeOrdner() {
313         return flAufnahmeOrdner;
314     }
315 
setFlAufnahmeOrdner(File inpFlAufnahmeOrdner)316     protected void setFlAufnahmeOrdner(File inpFlAufnahmeOrdner) {
317         flAufnahmeOrdner = inpFlAufnahmeOrdner;
318     }
319 
deleteSelectedAufnahme()320     public void deleteSelectedAufnahme() {
321         try {
322             // int nr = aTableAufnahmen.getSelectedRow();
323             GAufnahmeIF tmpAufnahme = getSelectedAufnahme();
324             if (tmpAufnahme == null) {
325                 return;
326             }
327             File tmpCridFile = new File(tmpAufnahme.getCridFilename());
328             String tmpWarning = "<html>" + tmpAufnahme.getTitel() + "<hr>"
329                     + GStrings.getString("gdialog.delete.text") + " "
330                     + flAufnahmeOrdner + " :<br>" + tmpCridFile.getName()
331                     + "<br>";
332             if (tmpAufnahme.istGesperrt()) {
333                 tmpWarning += "<hr> "
334                         + GStrings.getString("gdialog.delete.locked") + " <hr>";
335             }
336             tmpWarning += "</html>";
337 
338             Toolkit.getDefaultToolkit().beep();
339             if (JOptionPane.showConfirmDialog(null, tmpWarning, GStrings
340                     .getString("gdialog.delete"), JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) // ;if(false)
341             {
342                 boolean ok = tmpCridFile.delete();
343                 //
344                 aAufnahmen.remove(tmpAufnahme);
345                 // aTableModel.removeRow(nr);
346                 // aTableModel.fireTableDataChanged();
347                 GConsole.setInfo(tmpCridFile.getName() + (ok ? " " : " NOT ")
348                         + "deleted");
349                 sortiereAufnahmen(getSortSelector());
350             }
351         } catch (Exception e) {
352             GConsole.setErrorMessage(e);
353         }
354     }
355 
doEnabled()356     public void doEnabled() {
357         GDialog tmpDialog = getDialog();
358         int index = getTableAufnahmen().getSelectedRow();
359         boolean enable = (index >= 0);
360         tmpDialog.btnPrintEPG.setEnabled(enable);
361         tmpDialog.aItemPlayMPC.setEnabled(enable);
362         tmpDialog.aItemLoeschen.setEnabled(enable);
363         tmpDialog.aItemReread.setEnabled(enable);
364         tmpDialog.btnAuswaehlen.setEnabled(enable);
365         tmpDialog.aItemAuswaehlen.setEnabled(enable);
366         tmpDialog.btnHinzufuegen.setEnabled(enable);
367         tmpDialog.aItemHinzufuegen.setEnabled(enable);
368         tmpDialog.zeigeInfos(index);
369     }
370 
371     /**
372      *
373      * @return List of File (fmpg-directories)
374      */
getUsedRecDirectories()375     public List getUsedRecDirectories() {
376         List tmpList = null;
377         int anza = (aAufnahmen == null) ? 0 : aAufnahmen.size();
378         for (int i = 0; i < anza; i++) {
379             GAufnahmeIF tmpAufnahmeIF = (GAufnahmeIF) aAufnahmen.get(i);
380             if (tmpAufnahmeIF instanceof GAufnahmeLocal) {
381                 GAufnahmeLocal tmpAufnahme = (GAufnahmeLocal) tmpAufnahmeIF;
382                 File[] tmpFiles = tmpAufnahme.getFmpgDirectories();
383                 int anzf = (tmpFiles == null) ? 0 : tmpFiles.length;
384                 for (int j = 0; j < anzf; j++) {
385                     File tmpFile = tmpFiles[j];
386                     if (tmpFile == null) {
387                         continue;
388                     }
389                     if (tmpList == null) {
390                         tmpList = new ArrayList();
391                     }
392                     tmpList.add(tmpFile);
393                     GConsole.setMessage("Used dir: "
394                             + tmpFile.getAbsolutePath());
395                 } // for j
396             } // if Local
397         } // for i
398         return tmpList;
399     }
400 
401     /**
402      *
403      * @param inpList
404      *            List of File (fmpg-dirs)
405      * @return File (.rec-directory)
406      */
getLostDirectories(List inpList)407     public Object getLostDirectories(List inpList) {
408         File tmpRecDir = null;
409         try {
410             GConsole.setMessage("*** Start searching lost directories ***");
411             List inpUsedDirList = getUsedRecDirectories();
412             if (inpUsedDirList == null) {
413                 return null;
414             }
415             int anzd = (inpUsedDirList == null) ? 0 : inpUsedDirList.size();
416             tmpRecDir = getFlAufnahmeOrdner();
417             if (tmpRecDir == null) {
418                 return null;
419             }
420             String tmpRecVerzeichnis = tmpRecDir.getAbsolutePath();
421             if (!tmpRecVerzeichnis.endsWith(File.separator)) {
422                 tmpRecVerzeichnis += File.separator;
423             }
424             tmpRecVerzeichnis += ".rec" + File.separator;
425             tmpRecDir = new File(tmpRecVerzeichnis);
426             if (!tmpRecDir.exists()) {
427                 GConsole.setMessage("No directory " + tmpRecVerzeichnis
428                         + " found!");
429                 return null;
430             }
431             //
432             String tmpDirs[] = tmpRecDir.list(new FilenameFilter() {
433                 public boolean accept(File f, String s) {
434                     if (!f.isDirectory()) {
435                         return false;
436                     }
437                     return s.toLowerCase().endsWith(".fmpg");
438                 }
439             });
440 
441             // change name to file
442             List tmpList = null;
443             anzd = (tmpDirs == null) ? 0 : tmpDirs.length;
444             GConsole.setMessage(anzd + " fmpg-files found in .rec-dir");
445             for (int i = 0; i < anzd; i++) {
446                 String tmpFN = tmpRecVerzeichnis + tmpDirs[i];
447                 File tmpFile = new File(tmpFN);
448                 if (tmpList == null) {
449                     tmpList = new ArrayList();
450                 }
451                 tmpList.add(tmpFile);
452                 GConsole.setInfo(tmpFN);
453             } // for
454 
455             inpList.clear();
456             // nun vergleichen
457             for (int i = 0; i < anzd; i++) {
458                 File tmpFile = (File) tmpList.get(i);
459                 if (!inpUsedDirList.contains(tmpFile)) {
460                     inpList.add(tmpFile);
461                     GConsole.setMessage("Lost dir: "
462                             + tmpFile.getAbsolutePath());
463                 }
464             } // for i
465             anzd = (inpList == null) ? 0 : inpList.size();
466             GConsole.setMessage(anzd + " lost directories found");
467             GConsole.setMessage("*** End of searching lost directories ***");
468         } catch (Exception e) {
469             GConsole.setErrorMessage(e);
470         }
471         return tmpRecDir;
472 
473     }
474 
475     /**
476      * @return fileComparator
477      */
getFileComparator()478     private FileComparator getFileComparator() {
479         if (aFileComparator == null) {
480             aFileComparator = new FileComparator();
481         }
482         return aFileComparator;
483     }
484 
485 }
486