Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

simple_file_transfer.cpp

Go to the documentation of this file.
00001 
00008 /*
00009     This file is part of Advanced Strategic Command; http://www.asc-hq.de
00010     Copyright (C) 1994-2005  Martin Bickel  and  Marc Schellenberger
00011 
00012     This program is free software; you can redistribute it and/or modify
00013     it under the terms of the GNU General Public License as published by
00014     the Free Software Foundation; either version 2 of the License, or
00015     (at your option) any later version.
00016 
00017     This program is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020     GNU General Public License for more details.
00021 
00022     You should have received a copy of the GNU General Public License
00023     along with this program; see the file COPYING. If not, write to the 
00024     Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
00025     Boston, MA  02111-1307  USA
00026 */
00027 
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 #include <cstring>
00031 
00032 #include "../global.h"
00033 #include "../misc.h"
00034 #include "../loaders.h"
00035 #include "../gamemap.h"
00036 #include "../dlg_box.h"
00037 #include "../dialog.h"
00038 #include "simple_file_transfer.h"
00039 #include "../dialogs/fileselector.h"
00040 #include "../gameoptions.h"
00041 #include "../basestrm.h"
00042 
00043 void FileTransfer::readChildData ( tnstream& stream )
00044 {
00045    stream.readInt();
00046    filename = stream.readString();
00047 }
00048 
00049 void FileTransfer::writeChildData ( tnstream& stream ) const
00050 {
00051    stream.writeInt( 1 );
00052    stream.writeString( filename );
00053 }
00054 
00055 void FileTransfer::setup()
00056 {
00057    enterfilename();
00058 }
00059 
00060 
00061 void FileTransfer::setup( const ASCString& filename )
00062 {
00063    this->filename = filename;
00064 }
00065 
00066 bool FileTransfer::enterfilename()
00067 {
00068    filename = editString( "Enter Filename", filename );
00069    return true;
00070 }
00071 
00072 
00073 void FileTransfer::send( const GameMap* map, int lastPlayer, int lastturn )
00074 {
00075    while ( filename.empty() ) {
00076       if( !enterfilename() )
00077          return;
00078    }
00079 
00080    try {      
00081       ASCString fname = constructFileName( map, lastPlayer, lastturn );
00082 
00083       tfindfile ff( fname );
00084       tfindfile::FileInfo fi;
00085       if ( ff.getnextname( fi ) && fi.directoryLevel == 0 )
00086          do {
00087             if (choice_dlg("overwrite " + fname,"~y~es","~n~o") == 2) 
00088                fname = selectFile( ASCString("*") + tournamentextension, false );
00089          } while (fname.empty());
00090 
00091       {
00092          tnfilestream gamefile ( fname, tnstream::writing );
00093          tnetworkloaders nwl;
00094          nwl.savenwgame( &gamefile, map );
00095       }
00096 
00097       int nextPlayer = map->actplayer;
00098       if ( nextPlayer < 0 )
00099          nextPlayer = 0;
00100       
00101       if ( CGameOptions::Instance()->mailProgram.empty() ) {
00102          
00103          ASCString msg = "Data written!\nPlease send " + fname + " to \n" + map->player[nextPlayer].getName();
00104          if ( !map->player[nextPlayer].email.empty() )
00105             msg += " (" + map->player[nextPlayer].email + ")";
00106          infoMessage( msg );
00107       } else {
00108          
00109          ASCString fullFile = ::constructFileName(0, "", fname);
00110          
00111          ASCString command = CGameOptions::Instance()->mailProgram;
00112          command.replaceAll_ci( "$(fullfile)", fullFile);
00113          command.replaceAll_ci( "$(file)", fname );
00114          
00115          ASCString url = fullFile;
00116          if ( pathdelimitterstring != "/")
00117             url.replaceAll(pathdelimitterstring, "/");
00118          
00119          if ( url.find('/') == 0 )
00120             url = "file://" + url;
00121          else
00122             url = "file:///" + url;
00123 
00124          command.replaceAll_ci( "$(url)", url );
00125          
00126          if ( map->player[nextPlayer].email.empty() )
00127             command.replaceAll_ci( "$(to)", "unknown");
00128          else
00129             command.replaceAll_ci( "$(to)", map->player[nextPlayer].email );
00130          
00131          int sv = -1;
00132          for ( int i = 0; i < map->getPlayerCount(); ++i)
00133             if ( map->getPlayer(i).stat == Player::supervisor ) {
00134                sv = i;
00135                break;
00136             }
00137            
00138          if ( sv != -1 && sv != nextPlayer )
00139             command.replaceAll_ci( "$(sv)", map->player[sv].email );
00140          else
00141             command.replaceAll_ci( "$(sv)", "" );
00142          
00143          StatusMessageWindowHolder smw = MessagingHub::Instance().infoMessageWindow( "Executing external mailer:\n" + command );
00144          
00145 #ifdef _WIN32_
00146          // Windows sucks!
00147          ASCString realCommand = "\"" + command + "\"";
00148 #else
00149          ASCString& realCommand = command;
00150 #endif
00151 
00152          int res = system(realCommand.c_str());
00153          if (res != 0 ) {
00154             smw.close();
00155             errorMessage("Program failed with exit code " + ASCString::toString(res) + "\nCommand was:\n" + command);
00156          } else {
00157             smw.close();
00158             infoMessage( "Mail submitted successfully" );
00159          }
00160       }
00161    } catch ( tfileerror ) {
00162       errorMessage ( "error writing file " + filename );
00163    }
00164 }
00165 
00166 
00167 GameMap* FileTransfer::receive()
00168 {
00169    return loadPBEMFile( filename );
00170 }   
00171 
00172 GameMap* FileTransfer::loadPBEMFile( const ASCString& filename )
00173 {
00174    GameMap* map = NULL;
00175    try {      
00176       tnfilestream gamefile ( filename, tnstream::reading );
00177       tnetworkloaders nwl;
00178       map = nwl.loadnwgame( &gamefile );
00179       if ( map->actplayer < 0 )
00180          throw ASCmsgException("this map has not been properly started");
00181    } 
00182    catch ( tinvalidversion iv ) {
00183       throw iv;
00184    }
00185    catch ( tfileerror ) {
00186       errorMessage ( filename + " is not a legal email game" );
00187       return NULL;
00188    }
00189    return map;
00190 }
00191 
00192 ASCString FileTransfer::constructFileName( const GameMap* actmap, int lastPlayer, int lastturn ) const
00193 {
00194    ASCString s = filename;
00195    while ( s.find( "$p") != ASCString::npos ) {
00196       if ( lastPlayer >= 0 )
00197          s.replace( s.find( "$p"), 2, 1, 'A' + lastPlayer );
00198       else
00199          s.replace( s.find( "$p"), 2, "SV" );
00200    }
00201 
00202    while ( s.find( "$t") != ASCString::npos ) {
00203       if ( lastturn >= 0 )
00204          s.replace( s.find( "$t"), 2, ASCString::toString( lastturn ) );
00205       else
00206          s.replace( s.find( "$t"), 2, ASCString::toString ( actmap->time.turn() ) );
00207    }
00208 
00209    if ( !s.endswith(tournamentextension ))
00210       s += tournamentextension;
00211    return s;
00212 }
00213 
00214 
00215 namespace {
00216    const bool r1 = networkTransferMechanismFactory::Instance().registerClass( FileTransfer::mechanismID(), ObjectCreator<GameTransferMechanism, FileTransfer> );
00217 }
00218 

Generated on Tue Jun 24 01:27:52 2008 for Advanced Strategic Command by  doxygen 1.4.2