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.util.ArrayList; 22 import java.util.Collections; 23 import java.util.Comparator; 24 import java.util.HashMap; 25 import java.util.List; 26 27 import javax.swing.JButton; 28 import javax.swing.JOptionPane; 29 import javax.swing.JPanel; 30 import javax.swing.ListSelectionModel; 31 import javax.swing.event.ChangeEvent; 32 import javax.swing.event.ChangeListener; 33 import javax.swing.event.ListSelectionEvent; 34 import javax.swing.event.ListSelectionListener; 35 36 import net.sourceforge.dvb.projectx.common.Common; 37 import net.sourceforge.dvb.projectx.gui.FtpChooser; 38 import net.sourceforge.dvb.projectx.xinput.XInputDirectory; 39 import net.sourceforge.dvb.projectx.xinput.XInputFile; 40 import net.sourceforge.dvb.projectx.xinput.ftp.FtpVO; 41 42 import org.apache.commons.net.ftp.FTPClient; 43 44 /** 45 * 46 * 47 * @author arnaud 48 * 49 */ 50 public class GPanelFTP extends GPanelAbstract implements ChangeListener { 51 /** 52 * 53 */ 54 private static final long serialVersionUID = 1L; 55 56 // ---------------------------------------------------------- 57 public static class XInputFileComparatorAZ implements Comparator { 58 compare(Object arg0, Object arg1)59 public int compare(Object arg0, Object arg1) { 60 try { 61 XInputFile tmpFile0 = (XInputFile) arg0; 62 XInputFile tmpFile1 = (XInputFile) arg1; 63 String tmpFN0 = tmpFile0.getName(); 64 String tmpFN1 = tmpFile1.getName(); 65 return tmpFN0.compareTo(tmpFN1); 66 } catch (Exception e) { 67 // ignore 68 } 69 return 0; 70 } 71 72 } 73 74 // ---------------------------------------------------------- 75 class XInputFileComparatorZA implements Comparator { 76 compare(Object arg0, Object arg1)77 public int compare(Object arg0, Object arg1) { 78 try { 79 return -(getXFileComparatorAZ().compare(arg0, arg1)); 80 } catch (Exception e) { 81 // ignore 82 } 83 return 0; 84 } 85 86 } 87 88 // ---------------------------------------------------------- 89 90 protected JButton btnFtpChooser = new JButton(GStrings 91 .getString("gdialog.ftp.chooser")); 92 93 private JButton btnRefresh = new JButton(GStrings 94 .getString("gdialog.refresh")); 95 96 protected XInputDirectory aCridDirectory = null; 97 98 private XInputFileComparatorAZ aXFileComparatorAZ = null; 99 100 private XInputFileComparatorZA aXFileComparatorZA = null; 101 102 private GFTPHandler aFTPHandler = null; 103 GPanelFTP(GDialog inpDialog)104 public GPanelFTP(GDialog inpDialog) { 105 super(); 106 setDialog(inpDialog); 107 buildPanel(); 108 } 109 buildPanel()110 private void buildPanel() { 111 setLayout(new BorderLayout()); 112 // Buttons top 113 JPanel tmpPanelTop = new JPanel(new GridLayout(1, 2)); 114 tmpPanelTop.add(btnFtpChooser); 115 tmpPanelTop.add(btnRefresh); 116 btnRefresh.setEnabled(true); 117 // Buttons oben 118 add(tmpPanelTop, BorderLayout.NORTH); 119 // Aufnahmetabelle 120 add(spAufnahmen, BorderLayout.CENTER); 121 122 ListSelectionModel tmpSelectionModel = aTableAufnahmen 123 .getSelectionModel(); 124 tmpSelectionModel.addListSelectionListener(new ListSelectionListener() { 125 public void valueChanged(ListSelectionEvent e) { 126 if (!e.getValueIsAdjusting()) { 127 doEnabled(); 128 } 129 } 130 131 }); 132 133 aTableAufnahmen.addMouseListener(new MouseAdapter() { 134 public void mouseClicked(MouseEvent event) { 135 mouseClick(event); 136 } 137 }); 138 139 btnFtpChooser.addActionListener(new ActionListener() { 140 public void actionPerformed(ActionEvent e) { 141 connect(); 142 } 143 }); 144 145 btnRefresh.addActionListener(new ActionListener() { 146 public void actionPerformed(ActionEvent e) { 147 if (aCridDirectory == null) { 148 autoConnect(); 149 } else { 150 sucheAufnahmen(); 151 } 152 } 153 }); 154 155 } 156 157 /* 158 * (Kein Javadoc) 159 * 160 * @see de.m740.projectx.gigaset.GChooserIF#getSelectedRecording() 161 */ getSelectedRecording()162 public List getSelectedRecording() { 163 List tmpList = null; 164 try { 165 GAufnahmeIF tmpAufnahme = getSelectedAufnahme(); 166 if (tmpAufnahme == null) { 167 return null; 168 } 169 GConsole.setMessage("Get selected recording: " 170 + tmpAufnahme.getTitel()); 171 tmpList = tmpAufnahme.getMpgFiles(); 172 if (tmpList != null && tmpList.size() > 1) { 173 Collections.sort(tmpList, getXFileComparatorAZ()); 174 } 175 } catch (Exception e) { 176 GConsole.setErrorMessage(e); 177 } 178 return tmpList; 179 } 180 connect()181 protected void connect() { 182 try { 183 // Add ftp server directory to autoload list 184 FtpChooser tmpChooser = new FtpChooser(); 185 186 tmpChooser.pack(); 187 tmpChooser.setVisible(true); 188 189 XInputDirectory tmpXInputDirectory = tmpChooser 190 .getXInputDirectory(); 191 aCridDirectory = tmpXInputDirectory; 192 193 if (tmpChooser.isTested() && tmpXInputDirectory != null) { 194 sucheAufnahmen(); 195 } 196 197 } catch (Exception e) { 198 GConsole.setErrorMessage(e); 199 } 200 } 201 autoConnect2()202 protected boolean autoConnect2() { 203 boolean isTested = false; 204 try { 205 GConsole.setMessage("Try to connect to ftp-server..."); 206 207 final String tmpBaseKey = "FtpServer."; 208 209 String tmpServer = Common.getSettings().getProperty( 210 tmpBaseKey + "Server"); 211 212 String tmpPort = Common.getSettings().getProperty( 213 tmpBaseKey + "Port"); 214 215 String tmpUser = Common.getSettings().getProperty( 216 tmpBaseKey + "User"); 217 218 String tmpPassword = Common.getSettings().getProperty( 219 tmpBaseKey + "Password"); 220 221 String tmpDirectory = Common.getSettings().getProperty( 222 tmpBaseKey + "Directory"); 223 224 FtpVO ftpVO = new FtpVO(tmpServer, tmpUser, tmpPassword, 225 tmpDirectory, tmpPort, null); 226 XInputDirectory tmpXInputDirectory = new XInputDirectory(ftpVO); 227 228 isTested = tmpXInputDirectory.test(); 229 GConsole.setMessage(tmpXInputDirectory.getLog()); 230 GConsole.setMessage(tmpXInputDirectory.getTestMsg()); 231 232 aCridDirectory = tmpXInputDirectory; 233 234 if (isTested) { 235 sucheAufnahmen0(); 236 } 237 return isTested; 238 239 } catch (Exception e) { 240 GConsole.setErrorMessage(e); 241 } finally { 242 if (!isTested) { 243 btnFtpChooser.setEnabled(true); 244 } 245 } 246 return false; 247 } 248 249 /** 250 * 251 * @return true/false 252 */ isAutoConnectOn()253 public boolean isAutoConnectOn() { 254 try { 255 if (Common.getSettings().getBooleanProperty( 256 GDialog.PROPERTY_FTP_AUTOCONNECT, false)) { 257 return true; 258 } 259 } catch (Exception e) { 260 GConsole.setErrorMessage(e); 261 } 262 return false; 263 } 264 265 /** 266 * 267 * 268 */ autoConnect()269 public void autoConnect() { 270 try { 271 if (!isAutoConnectOn()) { 272 return; 273 } 274 Runnable tmpRunnable = new Runnable() { 275 public void run() { 276 autoConnect2(); 277 } 278 }; 279 btnFtpChooser.setEnabled(false); 280 btnRefresh.setEnabled(false); 281 if (!EventQueue.isDispatchThread()) { 282 EventQueue.invokeLater(tmpRunnable); 283 } else { 284 new Thread(tmpRunnable).start(); 285 } 286 } catch (Exception e) { 287 GConsole.setErrorMessage(e); 288 } 289 } 290 sucheAufnahmen0()291 private void sucheAufnahmen0() { 292 try { 293 btnFtpChooser.setEnabled(false); 294 btnRefresh.setEnabled(false); 295 final XInputDirectory tmpXInputDirectory = aCridDirectory; 296 297 if (tmpXInputDirectory != null) { 298 299 Runnable tmpRunnable = new Runnable() { 300 public void run() { 301 sucheAufnahmen1(tmpXInputDirectory); 302 } 303 304 }; 305 new Thread(tmpRunnable).start(); 306 } 307 308 } catch (Exception e) { 309 GConsole.setErrorMessage(e); 310 } 311 } 312 sucheAufnahmen()313 public void sucheAufnahmen() { 314 try { 315 GConsole.clear(); 316 sucheAufnahmen0(); 317 } catch (Exception e) { 318 GConsole.setErrorMessage(e); 319 } 320 } 321 sucheAufnahmen2(XInputDirectory inpDirectory)322 protected void sucheAufnahmen2(XInputDirectory inpDirectory) { 323 clearSelection(); 324 aTableModel.clear(); 325 aTableModel.fireTableDataChanged(); 326 fireStateChanged(); 327 if (inpDirectory == null) { 328 return; 329 } 330 // 331 ListSelectionModel tmpListSelectionModel = aTableAufnahmen 332 .getSelectionModel(); 333 // aTableAufnahmen = new GTable(aTableModel); 334 aTableAufnahmen.setSelectionModel(tmpListSelectionModel); 335 spAufnahmen.setViewportView(aTableAufnahmen); 336 // 337 XInputFile[] tmpFiles = inpDirectory.getFiles(); 338 int anzf = (tmpFiles == null) ? 0 : tmpFiles.length; 339 List tmpCridList = null; 340 for (int i = 0; i < anzf; i++) { 341 XInputFile tmpFile = tmpFiles[i]; 342 String tmpFN = tmpFile.getName(); 343 if (tmpFN.toLowerCase().endsWith(".crid")) { 344 if (tmpCridList == null) { 345 tmpCridList = new ArrayList(); 346 } 347 tmpCridList.add(tmpFile); 348 } 349 } // for i 350 tmpFiles = null; 351 if (tmpCridList == null) { 352 return; 353 } 354 anzf = tmpCridList.size(); 355 if (anzf > 1) { 356 Collections.sort(tmpCridList, getXFileComparatorZA()); 357 setSortSelector(SORT_FILES_NO); 358 } 359 aAufnahmen = new ArrayList(anzf); 360 HashMap tmpHash = new HashMap(anzf); 361 for (int i = 0; i < anzf; i++) { 362 try { 363 XInputFile tmpFile = (XInputFile) tmpCridList.get(i); 364 GAufnahmeFTP tmpAufnahme = new GAufnahmeFTP(tmpFile, 365 inpDirectory); 366 if (tmpAufnahme.isValid()) { 367 aAufnahmen.add(tmpAufnahme); 368 addAufnahme(i, tmpAufnahme, tmpHash); 369 // Listener anmelden, 370 // der uns �ber das Einlesen der MPGs informiert 371 tmpAufnahme.addChangeListener(this); 372 } else { 373 GConsole.setMessage("Invalid file " + tmpFile.getName() 374 + " skipped!"); 375 } 376 } catch (Exception exc) { 377 GConsole.setErrorMessage(exc); 378 } 379 } // for i 380 countChildren(GPanelTableIF.SORT_FILES_ON); 381 // evtl letzte Tree-�nderungen sichtbar machen 382 aTableModel.fireTableRowsUpdated(0, aTableModel.getRowCount() - 1); 383 tmpHash = null; 384 385 if (aTableModel.getRowCount() == 0) { 386 // falls keine Aufnahmen vorh. 387 getDialog().zeigeInfos(-1); // soll auch nix angezeigt w.! 388 } 389 } 390 391 /** 392 * 393 * @param inpAufnahmeOrdner 394 */ sucheAufnahmen1(XInputDirectory inpDirectory)395 protected void sucheAufnahmen1(XInputDirectory inpDirectory) { 396 try { 397 sucheAufnahmen2(inpDirectory); 398 } catch (Exception e) { 399 GConsole.setErrorMessage(e); 400 } finally { 401 btnFtpChooser.setEnabled(true); 402 btnRefresh.setEnabled(true); 403 } 404 } 405 deleteSelectedAufnahme()406 public void deleteSelectedAufnahme() { 407 try { 408 GAufnahmeIF tmpAufnahme = getSelectedAufnahme(); 409 if (tmpAufnahme == null) { 410 return; 411 } 412 // int nr = aTableAufnahmen.getSelectedRow(); 413 XInputFile tmpCridFile = ((GAufnahmeFTP) tmpAufnahme).getCridFile(); 414 String tmpWarning = "<html>" + tmpAufnahme.getTitel() + "<hr>" 415 + GStrings.getString("gdialog.delete.text") + " " 416 + aCridDirectory.getDirectory() + " :<br>" 417 + tmpCridFile.getName() + "<br>"; 418 if (tmpAufnahme.istGesperrt()) { 419 tmpWarning += "<hr> " 420 + GStrings.getString("gdialog.delete.locked") + " <hr>"; 421 } 422 tmpWarning += "</html>"; 423 424 Toolkit.getDefaultToolkit().beep(); 425 if (JOptionPane.showConfirmDialog(null, tmpWarning, GStrings 426 .getString("gdialog.delete"), JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) { 427 return; 428 } 429 GFTPHandler tmpHandler = getFTPHandler(); 430 FtpVO tmpVO = tmpHandler.makeVOFrom(aCridDirectory); 431 String tmpWD = aCridDirectory.getDirectory(); 432 FTPClient tmpClient = tmpHandler.openClient(tmpVO); 433 boolean ok = tmpClient.changeWorkingDirectory(tmpWD); 434 String tmpFN = tmpCridFile.getName(); 435 if (ok) { 436 ok = tmpClient.deleteFile(tmpFN); 437 if (ok) { 438 aAufnahmen.remove(tmpAufnahme); 439 // aTableModel.removeRow(nr); 440 // aTableModel.fireTableRowsDeleted(nr, nr); 441 sortiereAufnahmen(getSortSelector()); 442 } 443 } 444 tmpHandler.closeClient(tmpClient); 445 GConsole.setInfo(tmpFN + (ok ? " " : " NOT ") + "deleted"); 446 } catch (Exception e) { 447 GConsole.setErrorMessage(e); 448 } 449 } 450 doEnabled()451 public void doEnabled() { 452 GDialog tmpDialog = getDialog(); 453 int index = aTableAufnahmen.getSelectedRow(); 454 boolean enable = (index >= 0); 455 tmpDialog.btnPrintEPG.setEnabled(enable); 456 tmpDialog.aItemPlayMPC.setEnabled(false); 457 tmpDialog.aItemLoeschen.setEnabled(enable); 458 tmpDialog.aItemReread.setEnabled(enable); 459 tmpDialog.btnAuswaehlen.setEnabled(enable); 460 tmpDialog.aItemAuswaehlen.setEnabled(enable); 461 tmpDialog.btnHinzufuegen.setEnabled(enable); 462 tmpDialog.aItemHinzufuegen.setEnabled(enable); 463 tmpDialog.zeigeInfos(index); 464 } 465 getXFileComparatorZA()466 private XInputFileComparatorZA getXFileComparatorZA() { 467 if (aXFileComparatorZA == null) { 468 aXFileComparatorZA = new XInputFileComparatorZA(); 469 } 470 return aXFileComparatorZA; 471 } 472 getXFileComparatorAZ()473 protected XInputFileComparatorAZ getXFileComparatorAZ() { 474 if (aXFileComparatorAZ == null) { 475 aXFileComparatorAZ = new XInputFileComparatorAZ(); 476 } 477 return aXFileComparatorAZ; 478 } 479 480 /** 481 * 482 */ getUsedRecDirectories()483 public List getUsedRecDirectories() { 484 List tmpList = null; 485 int anza = (aAufnahmen == null) ? 0 : aAufnahmen.size(); 486 for (int i = 0; i < anza; i++) { 487 GAufnahmeIF tmpAufnahmeIF = (GAufnahmeIF) aAufnahmen.get(i); 488 if (tmpAufnahmeIF instanceof GAufnahmeFTP) { 489 GAufnahmeFTP tmpAufnahme = (GAufnahmeFTP) tmpAufnahmeIF; 490 XInputDirectory[] tmpDirs = tmpAufnahme 491 .getFmpgDirectories(true); 492 int anzd = (tmpDirs == null) ? 0 : tmpDirs.length; 493 for (int j = 0; j < anzd; j++) { 494 XInputDirectory tmpDir = tmpDirs[j]; 495 if (tmpDir == null) { 496 continue; 497 } 498 if (tmpList == null) { 499 tmpList = new ArrayList(); 500 } 501 tmpList.add(tmpDir); 502 GConsole.setMessage("Used dir: " + tmpDir.getDirectory()); 503 } // for j 504 } // if FTP 505 } // for i 506 return tmpList; 507 } 508 makeRecDirectory(String inpDateinamenbasis)509 private XInputDirectory makeRecDirectory(String inpDateinamenbasis) { 510 try { 511 XInputDirectory tmpXDirectory = aCridDirectory; 512 String tmpAufnahmeverzeichnis = tmpXDirectory.getDirectory(); 513 String tmpServer = tmpXDirectory.getServer(); 514 String tmpUser = tmpXDirectory.getUser(); 515 String tmpPassword = tmpXDirectory.getPassword(); 516 String tmpDirectory = tmpAufnahmeverzeichnis; 517 String tmpLim = "/"; 518 if (tmpDirectory.indexOf('\\') >= 0) { 519 //tmpLim = "\\"; 520 } 521 if (!tmpDirectory.endsWith(tmpLim)) { 522 tmpDirectory += tmpLim; 523 } 524 tmpDirectory += ".rec"; 525 String tmpPort = tmpXDirectory.getPort(); 526 FtpVO tmpVO = new FtpVO(tmpServer, tmpUser, tmpPassword, 527 tmpDirectory, tmpPort, null); 528 XInputDirectory tmpIDirectory = new XInputDirectory(tmpVO); 529 if (tmpIDirectory.test()) { 530 return tmpIDirectory; 531 } 532 } catch (Exception e) { 533 GConsole.setErrorMessage(e); 534 } 535 return null; 536 } 537 missing(List inpList, Object inpObj)538 private boolean missing(List inpList, Object inpObj) { 539 try { 540 String tmpFN = inpObj.toString(); 541 int anz = (inpList == null) ? 0 : inpList.size(); 542 for (int i = 0; i < anz; i++) { 543 Object tmpObj = inpList.get(i); 544 String tmpStr = (tmpObj == null) ? "" : tmpObj.toString(); 545 if (tmpStr.equals(tmpFN)) { 546 return false; 547 } 548 } // for i 549 return true; 550 } catch (Exception e) { 551 GConsole.setErrorMessage(e); 552 } 553 return false; // error 554 } 555 556 /** 557 * 558 * @param inpList 559 * List of File (fmpg-dirs) 560 * @return XInputDirectory (.rec-directory) 561 */ getLostDirectories(List inpList)562 public Object getLostDirectories(List inpList) { 563 XInputDirectory tmpRecDir = null; 564 try { 565 inpList.clear(); 566 tmpRecDir = aCridDirectory; 567 if (tmpRecDir == null) { 568 return null; 569 } 570 tmpRecDir = makeRecDirectory(tmpRecDir.getDirectory()); 571 if (tmpRecDir == null) { 572 return null; 573 } 574 // 575 GConsole.setMessage("*** Start searching lost directories ***"); 576 XInputFile[] tmpFilesInRecDir = new GFTPHandler().getFilesAndDirectories(tmpRecDir); 577 // 578 List tmpUsedDirList = getUsedRecDirectories(); 579 int anzd = (tmpUsedDirList == null) ? 0 : tmpUsedDirList.size(); 580 GConsole.setMessage(anzd + " used directories read"); 581 if (tmpUsedDirList == null) { 582 return null; 583 } 584 anzd = (tmpFilesInRecDir == null) ? 0 : tmpFilesInRecDir.length; 585 GConsole.setMessage(anzd + " files (all types) found in .rec-dir"); 586 // 587 // change array to list 588 List tmpList = null; 589 for (int i = 0; i < anzd; i++) { 590 XInputFile tmpFile = tmpFilesInRecDir[i]; 591 String tmpFN = tmpFile.getName(); 592 if (tmpFN.toLowerCase().endsWith(".fmpg")) { 593 if (tmpList == null) { 594 tmpList = new ArrayList(); 595 } 596 tmpList.add(tmpFile); 597 GConsole.setInfo(tmpFN); 598 } 599 } // for 600 601 anzd = (tmpList == null) ? 0 : tmpList.size(); 602 GConsole.setMessage(anzd + " fmpg-files found in .rec-dir"); 603 // nun vergleichen 604 for (int i = 0; i < anzd; i++) { 605 Object tmpObject = tmpList.get(i); 606 if (missing(tmpUsedDirList, tmpObject)) { 607 inpList.add(tmpObject); 608 GConsole.setMessage("Lost dir: " + tmpObject.toString()); 609 } 610 } // for i 611 anzd = (inpList == null) ? 0 : inpList.size(); 612 GConsole.setMessage(anzd + " lost directories found"); 613 GConsole.setMessage("*** End of searching lost directories ***"); 614 } catch (Exception e) { 615 GConsole.setErrorMessage(e); 616 } 617 return tmpRecDir; 618 619 } 620 621 /** 622 * @return fTPHandler 623 */ getFTPHandler()624 protected GFTPHandler getFTPHandler() { 625 if (aFTPHandler == null) { 626 aFTPHandler = new GFTPHandler(); 627 } 628 return aFTPHandler; 629 } 630 631 /* 632 * (Kein Javadoc) 633 * 634 * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent) 635 */ stateChanged(ChangeEvent inpE)636 public void stateChanged(ChangeEvent inpE) { 637 try { 638 // es sind MPGs fertig eingelesen worden 639 // (von irgendeiner(!) Aufnahme) 640 GDialog tmpDialog = getDialog(); 641 int index = aTableAufnahmen.getSelectedRow(); 642 tmpDialog.zeigeInfos(index); 643 aTableModel.fireTableRowsUpdated(0, aTableModel.getRowCount()); 644 } catch (Exception e) { 645 GConsole.setErrorMessage(e); 646 } 647 } 648 649 } 650