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-2010  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       bool mail = !CGameOptions::Instance()->mailProgram.empty();
00102 
00103 
00104       if ( mail )
00105          if (choiceDialog("Invoke the mail program to send the file?","~m~ail","~j~ust save", "sendpbem") == 2)
00106             mail = false;
00107 
00108 
00109       if ( !mail ) {
00110          
00111          ASCString msg = "Data written!\nPlease send " + fname + " to \n" + map->player[nextPlayer].getName();
00112          if ( !map->player[nextPlayer].email.empty() )
00113             msg += " (" + map->player[nextPlayer].email + ")";
00114          infoMessage( msg );
00115       } else {
00116          
00117          ASCString fullFile = ::constructFileName(0, "", fname);
00118          
00119          ASCString command = CGameOptions::Instance()->mailProgram;
00120          command.replaceAll_ci( "$(fullfile)", fullFile);
00121          command.replaceAll_ci( "$(file)", fname );
00122          
00123          ASCString url = fullFile;
00124          if ( pathdelimitterstring != ASCString("/"))
00125             url.replaceAll(pathdelimitterstring, "/");
00126          
00127          if ( url.find('/') == 0 )
00128             url = "file://" + url;
00129          else
00130             url = "file:///" + url;
00131 
00132          command.replaceAll_ci( "$(url)", url );
00133          
00134          if ( map->player[nextPlayer].email.empty() )
00135             command.replaceAll_ci( "$(to)", "unknown");
00136          else
00137             command.replaceAll_ci( "$(to)", map->player[nextPlayer].email );
00138          
00139          int sv = -1;
00140          for ( int i = 0; i < map->getPlayerCount(); ++i)
00141             if ( map->getPlayer(i).stat == Player::supervisor ) {
00142                sv = i;
00143                break;
00144             }
00145            
00146          if ( sv != -1 && sv != nextPlayer )
00147             command.replaceAll_ci( "$(sv)", map->player[sv].email );
00148          else
00149             command.replaceAll_ci( "$(sv)", "" );
00150          
00151          StatusMessageWindowHolder smw = MessagingHub::Instance().infoMessageWindow( "Executing external mailer:\n" + command );
00152          
00153 #ifdef _WIN32_
00154          // Windows sucks!
00155          ASCString realCommand = "\"" + command + "\"";
00156 #else
00157          ASCString& realCommand = command;
00158 #endif
00159 
00160          int res = system(realCommand.c_str());
00161          if (res != 0 ) {
00162             smw.close();
00163             errorMessage("Program failed with exit code " + ASCString::toString(res) + "\nCommand was:\n" + command);
00164          } else {
00165             smw.close();
00166             infoMessage( "Mail submitted successfully" );
00167          }
00168       }
00169    } catch ( tfileerror ) {
00170       errorMessage ( "error writing file " + filename );
00171    }
00172 }
00173 
00174 
00175 GameMap* FileTransfer::receive()
00176 {
00177    return loadPBEMFile( filename );
00178 }   
00179 
00180 GameMap* FileTransfer::loadPBEMFile( const ASCString& filename )
00181 {
00182    GameMap* map = NULL;
00183    try {      
00184       tnfilestream gamefile ( filename, tnstream::reading );
00185       tnetworkloaders nwl;
00186       map = nwl.loadnwgame( &gamefile );
00187       if ( map->actplayer < 0 )
00188          throw ASCmsgException("this map has not been properly started");
00189    } 
00190    catch ( tinvalidversion iv ) {
00191       throw iv;
00192    }
00193    catch ( tfileerror ) {
00194       errorMessage ( filename + " is not a legal email game" );
00195       return NULL;
00196    }
00197    return map;
00198 }
00199 
00200 ASCString FileTransfer::constructFileName( const GameMap* actmap, int lastPlayer, int lastturn ) const
00201 {
00202    ASCString s = filename;
00203    while ( s.find( "$p") != ASCString::npos ) {
00204       if ( lastPlayer >= 0 )
00205          s.replace( s.find( "$p"), 2, 1, 'A' + lastPlayer );
00206       else
00207          s.replace( s.find( "$p"), 2, "SV" );
00208    }
00209 
00210    while ( s.find( "$t") != ASCString::npos ) {
00211       if ( lastturn >= 0 )
00212          s.replace( s.find( "$t"), 2, ASCString::toString( lastturn ) );
00213       else
00214          s.replace( s.find( "$t"), 2, ASCString::toString ( actmap->time.turn() ) );
00215    }
00216 
00217    if ( !s.endswith(tournamentextension ))
00218       s += tournamentextension;
00219    return s;
00220 }
00221 
00222 
00223 namespace {
00224    const bool r1 = networkTransferMechanismFactory::Instance().registerClass( FileTransfer::mechanismID(), ObjectCreator<GameTransferMechanism, FileTransfer> );
00225 }
00226 

Generated on Mon May 21 01:26:37 2012 for Advanced Strategic Command by  doxygen 1.5.1