Advanced Strategic Command
simple_file_transfer.cpp
Go to the documentation of this file.
1 
8 /*
9  This file is part of Advanced Strategic Command; http://www.asc-hq.de
10  Copyright (C) 1994-2010 Martin Bickel and Marc Schellenberger
11 
12  This program is free software; you can redistribute it and/or modify
13  it under the terms of the GNU General Public License as published by
14  the Free Software Foundation; either version 2 of the License, or
15  (at your option) any later version.
16 
17  This program is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with this program; see the file COPYING. If not, write to the
24  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
25  Boston, MA 02111-1307 USA
26 */
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <cstring>
31 
32 #include "../global.h"
33 #include "../misc.h"
34 #include "../loaders.h"
35 #include "../gamemap.h"
36 #include "../dlg_box.h"
37 #include "../dialog.h"
38 #include "simple_file_transfer.h"
39 #include "../dialogs/fileselector.h"
40 #include "../gameoptions.h"
41 #include "../basestrm.h"
42 
44 {
45  stream.readInt();
46  filename = stream.readString();
47 }
48 
49 void FileTransfer::writeChildData ( tnstream& stream ) const
50 {
51  stream.writeInt( 1 );
52  stream.writeString( filename );
53 }
54 
56 {
57  enterfilename();
58 }
59 
60 
61 void FileTransfer::setup( const ASCString& filename )
62 {
63  this->filename = filename;
64 }
65 
67 {
68  filename = editString( "Enter Filename", filename );
69  return true;
70 }
71 
72 
73 void FileTransfer::send( const GameMap* map, int lastPlayer, int lastturn )
74 {
75  while ( filename.empty() ) {
76  if( !enterfilename() )
77  return;
78  }
79 
80  try {
81  ASCString fname = constructFileName( map, lastPlayer, lastturn );
82 
83  tfindfile ff( fname );
85  if ( ff.getnextname( fi ) && fi.directoryLevel == 0 )
86  do {
87  if (choice_dlg("overwrite " + fname,"~y~es","~n~o") == 2)
88  fname = selectFile( ASCString("*") + tournamentextension, false );
89  } while (fname.empty());
90 
91  {
92  tnfilestream gamefile ( fname, tnstream::writing );
93  tnetworkloaders nwl;
94  nwl.savenwgame( &gamefile, map );
95  }
96 
97  int nextPlayer = map->actplayer;
98  if ( nextPlayer < 0 )
99  nextPlayer = 0;
100 
101  bool mail = !CGameOptions::Instance()->mailProgram.empty();
102 
103 
104  if ( mail )
105  if (choiceDialog("Invoke the mail program to send the file?","~m~ail","~j~ust save", "sendpbem") == 2)
106  mail = false;
107 
108 
109  if ( !mail ) {
110 
111  ASCString msg = "Data written!\nPlease send " + fname + " to \n" + map->player[nextPlayer].getName();
112  if ( !map->player[nextPlayer].email.empty() )
113  msg += " (" + map->player[nextPlayer].email + ")";
114  infoMessage( msg );
115  } else {
116 
117  ASCString fullFile = ::constructFileName(0, "", fname);
118 
120  command.replaceAll_ci( "$(fullfile)", fullFile);
121  command.replaceAll_ci( "$(file)", fname );
122 
123  ASCString url = fullFile;
124  if ( pathdelimitterstring != ASCString("/"))
126 
127  if ( url.find('/') == 0 )
128  url = "file://" + url;
129  else
130  url = "file:///" + url;
131 
132  command.replaceAll_ci( "$(url)", url );
133 
134  if ( map->player[nextPlayer].email.empty() )
135  command.replaceAll_ci( "$(to)", "unknown");
136  else
137  command.replaceAll_ci( "$(to)", map->player[nextPlayer].email );
138 
139  int sv = -1;
140  for ( int i = 0; i < map->getPlayerCount(); ++i)
141  if ( map->getPlayer(i).stat == Player::supervisor ) {
142  sv = i;
143  break;
144  }
145 
146  if ( sv != -1 && sv != nextPlayer )
147  command.replaceAll_ci( "$(sv)", map->player[sv].email );
148  else
149  command.replaceAll_ci( "$(sv)", "" );
150 
151  StatusMessageWindowHolder smw = MessagingHub::Instance().infoMessageWindow( "Executing external mailer:\n" + command );
152 
153 #ifdef _WIN32_
154  // Windows sucks!
155  ASCString realCommand = "\"" + command + "\"";
156 #else
157  ASCString& realCommand = command;
158 #endif
159 
160  int res = system(realCommand.c_str());
161  if (res != 0 ) {
162  smw.close();
163  errorMessage("Program failed with exit code " + ASCString::toString(res) + "\nCommand was:\n" + command);
164  } else {
165  smw.close();
166  infoMessage( "Mail submitted successfully" );
167  }
168  }
169  } catch ( tfileerror ) {
170  errorMessage ( "error writing file " + filename );
171  }
172 }
173 
174 
176 {
177  return loadPBEMFile( filename );
178 }
179 
181 {
182  GameMap* map = NULL;
183  try {
184  tnfilestream gamefile ( filename, tnstream::reading );
185  tnetworkloaders nwl;
186  map = nwl.loadnwgame( &gamefile );
187  if ( map->actplayer < 0 )
188  throw ASCmsgException("this map has not been properly started");
189  }
190  catch ( tinvalidversion iv ) {
191  throw iv;
192  }
193  catch ( tfileerror ) {
194  errorMessage ( filename + " is not a legal email game" );
195  return NULL;
196  }
197  return map;
198 }
199 
200 ASCString FileTransfer::constructFileName( const GameMap* actmap, int lastPlayer, int lastturn ) const
201 {
202  ASCString s = filename;
203  while ( s.find( "$p") != ASCString::npos ) {
204  if ( lastPlayer >= 0 )
205  s.replace( s.find( "$p"), 2, 1, 'A' + lastPlayer );
206  else
207  s.replace( s.find( "$p"), 2, "SV" );
208  }
209 
210  while ( s.find( "$t") != ASCString::npos ) {
211  if ( lastturn >= 0 )
212  s.replace( s.find( "$t"), 2, ASCString::toString( lastturn ) );
213  else
214  s.replace( s.find( "$t"), 2, ASCString::toString ( actmap->time.turn() ) );
215  }
216 
217  if ( !s.endswith(tournamentextension ))
218  s += tournamentextension;
219  return s;
220 }
221 
222 
223 namespace {
224  const bool r1 = networkTransferMechanismFactory::Instance().registerClass( FileTransfer::mechanismID(), ObjectCreator<GameTransferMechanism, FileTransfer> );
225 }
226 
void send(const GameMap *map, int lastPlayer, int lastturn)
Player & getPlayer(PlayerID p)
Definition: gamemap.h:257
virtual void writeInt(int i)
Writes a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is p...
Definition: basestrm.cpp:363
GameMap * loadPBEMFile(const ASCString &filename)
virtual int readInt(void)
Reads a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is pe...
Definition: basestrm.cpp:284
ASCString getnextname(int *loc=NULL, bool *inContainer=NULL, ASCString *location=NULL)
Returns the next entry of the internal file list.
Definition: basestrm.cpp:2211
ASCString getName() const
returns the name of the player
Definition: player.cpp:274
int choiceDialog(const ASCString &text, const ASCString &button1, const ASCString &button2, const ASCString &shortLabel)
Definition: paradialog.cpp:806
int turn() const
Definition: typen.h:181
GameMap * loadnwgame(tnstream *strm)
Definition: loaders.cpp:1276
The interface for all kinds of IO stream.
static ASCString mechanismID()
A class that hols a MessageWindow. This class ensures that the window is removed if the last copy of ...
Definition: messaginghub.h:34
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
virtual void writeString(const string &pc, bool binary=true)
writes the C++ String pc to the stream.
Definition: basestrm.cpp:545
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
enum Player::PlayerStatus stat
Searching for files.
Definition: basestrm.h:484
ASCString editString(const ASCString &title, const ASCString &defaultValue)
Definition: dlg_box.cpp:3894
ASCString email
Definition: player.h:211
signed char actplayer
the player who is currently making his moves (may be human or AI)
Definition: gamemap.h:232
void readChildData(tnstream &stream)
Player player[9]
Definition: gamemap.h:253
void errorMessage(const ASCString &string)
static CGameOptions * Instance()
returns the only Instance
Definition: gameoptions.cpp:38
int savenwgame(tnstream *strm, const GameMap *gamemap)
Definition: loaders.cpp:1235
const char * pathdelimitterstring
Definition: unix/fileio.cpp:24
ASCString selectFile(const ASCString &ext, bool load, bool overwriteMessage)
virtual ASCString readString(bool includeCR=false)
Reads and returns a string.
Definition: basestrm.cpp:535
void writeChildData(tnstream &stream) const
GameMap * actmap
Definition: spfst.cpp:64
bool endswith(const ASCString &s) const
Checks if the last characters of string are equal to s.
Definition: ascstring.cpp:146
int choice_dlg(const ASCString &title, const ASCString &leftButton, const ASCString &rightButton)
Definition: dialog.cpp:616
ASCString & replaceAll(const ASCString &old, const ASCString &newString)
replaces all occurances of old with newString
Definition: ascstring.cpp:239
int getPlayerCount() const
Definition: gamemap.h:255
ASCString mailProgram
Definition: gameoptions.h:231
GameTime time
the time in the game, mesured in a turns and moves
Definition: gamemap.h:235
const char * tournamentextension
Definition: loaders.cpp:80
void infoMessage(const ASCString &string)
The map. THE central structure of ASC, which holds everything not globally available together...
Definition: gamemap.h:182
ASCString & replaceAll_ci(const ASCString &old, const ASCString &newString)
replaces all occurances of old with newString ; case insensitive
Definition: ascstring.cpp:248
ASCString constructFileName(const GameMap *actmap, int lastPlayer, int lastturn) const