001    /**
002     * iFish - An iRiver iHP jukebox database creation tool
003     *
004     * Copyright (C) 2009 Richard "Shred" Körber
005     *   http://ifish.shredzone.org
006     *
007     * This program is free software: you can redistribute it and/or modify
008     * it under the terms of the GNU General Public License as published by
009     * the Free Software Foundation, either version 3 of the License, or
010     * (at your option) any later version.
011     *
012     * This program is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015     * GNU General Public License for more details.
016     *
017     * You should have received a copy of the GNU General Public License
018     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
019     */
020    package net.shredzone.ifish.actions;
021    
022    import java.awt.event.ActionEvent;
023    import java.awt.event.KeyEvent;
024    import java.io.File;
025    
026    import javax.swing.KeyStroke;
027    
028    import net.shredzone.ifish.IFish;
029    import net.shredzone.ifish.IFishPane;
030    import net.shredzone.ifish.IFishRenameCallback;
031    import net.shredzone.ifish.db.PlaylistDb;
032    import net.shredzone.ifish.db.Sync;
033    import net.shredzone.ifish.gui.ExceptionDialog;
034    import net.shredzone.ifish.gui.NaviDbTableModel;
035    import net.shredzone.ifish.gui.StatusProgressBar;
036    import net.shredzone.ifish.i18n.L;
037    import net.shredzone.ifish.pool.ImgPool;
038    
039    /**
040     * Synchronizes a database from the current directory.
041     *
042     * @author  Richard Körber &lt;dev@shredzone.de&gt;
043     * @version $Id: SyncDatabaseAction.java 291 2009-04-28 21:29:27Z shred $
044     */
045    public class SyncDatabaseAction extends AsyncIFishAction {
046      private static final long serialVersionUID = 3256721775537828914L;
047    
048      /**
049       * Create a new SyncDatabaseAction.
050       *
051       * @param   fish        Related IFishPane
052       */
053      public SyncDatabaseAction( IFishPane fish ) {
054        super(
055          fish,
056          L.tr("action.db.sync"),
057          ImgPool.get("sync.png"),
058          L.tr("action.db.sync.tt"),
059          KeyStroke.getKeyStroke( KeyEvent.VK_Y, ActionEvent.CTRL_MASK )
060        );
061      }
062    
063      /**
064       * The action implementation itself. Do not call directly.
065       */
066      @Override
067      protected void action() {
068        //--- Get the Scanner tab to the foreground ---
069        fish.getAction( IFishPane.ACTION_TAB_SCANNER ).perform();
070        
071        //--- Get the parameters ---
072        final String dir       = prefs.getDirectory();
073        String charset         = prefs.getCharset();
074        final boolean compat   = prefs.isCompatibility();
075        final boolean rebuild  = prefs.isRebuild();
076        final boolean chkDate  = prefs.isCheckDate();
077        final boolean delMac   = prefs.isDelMac();
078        final boolean genPL    = prefs.isPLComment();
079        final boolean genNewPL = prefs.isPLNew();
080        final boolean chkTrack = prefs.isCheckTracks();
081        final String plname    = prefs.getPLName();
082        
083        final boolean doPlaylists = ( genPL || genNewPL );
084        
085        if( dir.equals("") ) return;
086    
087        if(! compat ) charset = "UTF-16";
088    
089        //--- Initialize the progress bar ---
090        StatusProgressBar progress = fish.getStatusBar();
091        progress.setAction( L.tr("a.syncdb.syncing") );
092        
093        //--- Get the Model ---
094        NaviDbTableModel model;
095        PlaylistDb       playlist = new PlaylistDb( fish.getPLBase() );
096        
097        if( rebuild ) {
098          // Create an entirely new database
099          model    = new NaviDbTableModel();
100          playlist = new PlaylistDb( fish.getPLBase() );
101          
102        }else {
103          // Update an existing database
104          
105          model    = fish.getDatabase();
106          if( doPlaylists ) {
107            playlist = fish.getPlaylist();
108            if( playlist==null )
109              playlist = new PlaylistDb( fish.getPLBase() );
110          }
111          fish.setDatabase( new NaviDbTableModel() );
112          fish.setPlaylist( new PlaylistDb( fish.getPLBase() ) );
113        }
114        
115        //--- Synchronize ---
116        try {
117          Sync sync;
118          if( doPlaylists ) {
119            sync = new Sync( model, playlist );
120          }else {
121            sync = new Sync( model );
122          }
123          sync.setCheckModified( chkDate );
124          sync.setCheckTrackNumbers( chkTrack );
125          sync.setDeleteResourceFork( delMac );
126          sync.setCreateCommentPlaylist( genPL );
127          sync.setCreateNewEntrantPlaylist( genNewPL );
128          sync.setNewEntrantName( plname );
129          File basedir = new File( dir );
130          File file = new File( basedir, "iRivNavi.iDB" );
131          model.resetCounters();
132          progress.setCountdown( true );
133          sync.syncDir( basedir, progress, new IFishRenameCallback( fish ) );
134          fish.updateStatus( model );
135          progress.setAction( L.tr("a.syncdb.saving") );
136          progress.setCountdown( false );
137          model.exportFile( file, charset, progress );
138          if( doPlaylists ) {
139            progress.setAction( L.tr("a.syncdb.plsaving") );
140            progress.setCountdown( false );
141            playlist.writePlaylists( basedir, charset, progress );
142            fish.setUnsaved( false );
143          }
144        }catch( Exception e ) {
145          ExceptionDialog.show( fish, L.tr("a.syncdb.error"), e );
146        }finally {
147          fish.updateStatus( model );
148          fish.setDatabase( model );
149          fish.setPlaylist( playlist );
150          progress.done();
151        }
152      }
153    
154    }
155