| 1 | /* |
| 2 | Copyright (C) 1999-2006 Id Software, Inc. and contributors. |
| 3 | For a list of contributors, see the accompanying CONTRIBUTORS file. |
| 4 | |
| 5 | This file is part of GtkRadiant. |
| 6 | |
| 7 | GtkRadiant is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | GtkRadiant is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with GtkRadiant; if not, write to the Free Software |
| 19 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #include <uilib/uilib.h> |
| 23 | #include "autosave.h" |
| 24 | #include "globaldefs.h" |
| 25 | |
| 26 | #include "os/file.h" |
| 27 | #include "os/path.h" |
| 28 | #include "cmdlib.h" |
| 29 | #include "stream/stringstream.h" |
| 30 | #include "gtkutil/messagebox.h" |
| 31 | #include "scenelib.h" |
| 32 | #include "mapfile.h" |
| 33 | |
| 34 | #include "map.h" |
| 35 | #include "mainframe.h" |
| 36 | #include "qe3.h" |
| 37 | #include "preferences.h" |
| 38 | |
| 39 | |
| 40 | #if GDEF_OS_WINDOWS |
| 41 | #define PATH_MAX 260 |
| 42 | #endif |
| 43 | |
| 44 | |
| 45 | bool DoesFileExist( const char* name, std::size_t& size ){ |
| 46 | if ( file_exists( name ) ) { |
| 47 | size += file_size( name ); |
| 48 | return true; |
| 49 | } |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | void Map_Snapshot(){ |
| 54 | // we need to do the following |
| 55 | // 1. make sure the snapshot directory exists (create it if it doesn't) |
| 56 | // 2. find out what the lastest save is based on number |
| 57 | // 3. inc that and save the map |
| 58 | const char* path = Map_Name( g_map ); |
| 59 | const char* name = path_get_filename_start( path ); |
| 60 | |
| 61 | StringOutputStream snapshotsDir( 256 ); |
| 62 | snapshotsDir << StringRange( path, name ) << "snapshots"; |
| 63 | |
| 64 | if ( file_exists( snapshotsDir.c_str() ) || Q_mkdir( snapshotsDir.c_str() ) ) { |
| 65 | std::size_t lSize = 0; |
| 66 | StringOutputStream strNewPath( 256 ); |
| 67 | strNewPath << snapshotsDir.c_str() << '/' << name; |
| 68 | |
| 69 | StringOutputStream snapshotFilename( 256 ); |
| 70 | |
| 71 | for ( int nCount = 0; ; ++nCount ) |
| 72 | { |
| 73 | // The original map's filename is "<path>/<name>.<ext>" |
| 74 | // The snapshot's filename will be "<path>/snapshots/<name>.<count>.<ext>" |
| 75 | const char* end = path_get_filename_base_end( strNewPath.c_str() ); |
| 76 | snapshotFilename << StringRange( strNewPath.c_str(), end ) << '.' << nCount << end; |
| 77 | |
| 78 | if ( !DoesFileExist( snapshotFilename.c_str(), lSize ) ) { |
| 79 | break; |
| 80 | } |
| 81 | |
| 82 | snapshotFilename.clear(); |
| 83 | } |
| 84 | |
| 85 | // save in the next available slot |
| 86 | Map_SaveFile( snapshotFilename.c_str() ); |
| 87 | |
| 88 | if ( lSize > 50 * 1024 * 1024 ) { // total size of saves > 50 mb |
| 89 | globalOutputStream() << "The snapshot files in " << snapshotsDir.c_str() << " total more than 50 megabytes. You might consider cleaning up."; |
| 90 | } |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | StringOutputStream strMsg( 256 ); |
| 95 | strMsg << "Snapshot save failed.. unabled to create directory\n" << snapshotsDir.c_str(); |
| 96 | ui::alert( MainFrame_getWindow(), strMsg.c_str() ); |
| 97 | } |
| 98 | } |
| 99 | /* |
| 100 | =============== |
| 101 | QE_CheckAutoSave |
| 102 | |
| 103 | If five minutes have passed since making a change |
| 104 | and the map hasn't been saved, save it out. |
| 105 | =============== |
| 106 | */ |
| 107 | |
| 108 | bool g_AutoSave_Enabled = true; |
| 109 | int m_AutoSave_Frequency = 15; |
| 110 | bool g_SnapShots_Enabled = false; |
| 111 | |
| 112 | namespace |
| 113 | { |
| 114 | time_t s_start = 0; |
| 115 | std::size_t s_changes = 0; |
| 116 | } |
| 117 | |
| 118 | void AutoSave_clear(){ |
| 119 | s_changes = 0; |
| 120 | } |
| 121 | |
| 122 | scene::Node& Map_Node(){ |
| 123 | return GlobalSceneGraph().root(); |
| 124 | } |
| 125 | |
| 126 | void QE_CheckAutoSave( void ){ |
| 127 | if ( !Map_Valid( g_map ) || !ScreenUpdates_Enabled() ) { |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | time_t now; |
| 132 | time( &now ); |
| 133 | |
| 134 | if ( s_start == 0 || s_changes == Node_getMapFile( Map_Node() )->changes() ) { |
| 135 | s_start = now; |
| 136 | } |
| 137 | |
| 138 | if ( ( now - s_start ) > ( 60 * m_AutoSave_Frequency ) ) { |
| 139 | s_start = now; |
| 140 | s_changes = Node_getMapFile( Map_Node() )->changes(); |
| 141 | |
| 142 | if ( g_AutoSave_Enabled ) { |
| 143 | const char* strMsg = g_SnapShots_Enabled ? "Autosaving snapshot..." : "Autosaving..."; |
| 144 | globalOutputStream() << strMsg << "\n"; |
| 145 | //Sys_Status(strMsg); |
| 146 | |
| 147 | // only snapshot if not working on a default map |
| 148 | if ( g_SnapShots_Enabled && !Map_Unnamed( g_map ) ) { |
| 149 | Map_Snapshot(); |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | if ( Map_Unnamed( g_map ) ) { |
| 154 | StringOutputStream autosave( 256 ); |
| 155 | autosave << g_qeglobals.m_userGamePath.c_str() << "maps/"; |
| 156 | Q_mkdir( autosave.c_str() ); |
| 157 | autosave << "autosave.map"; |
| 158 | Map_SaveFile( autosave.c_str() ); |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | const char* name = Map_Name( g_map ); |
| 163 | const char* extension = path_get_filename_base_end( name ); |
| 164 | StringOutputStream autosave( 256 ); |
| 165 | autosave << StringRange( name, extension ) << ".autosave" << extension; |
| 166 | Map_SaveFile( autosave.c_str() ); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | else |
| 171 | { |
| 172 | globalOutputStream() << "Autosave skipped...\n"; |
| 173 | //Sys_Status ("Autosave skipped..."); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void Autosave_constructPreferences( PreferencesPage& page ){ |
| 179 | ui::CheckButton autosave_enabled = page.appendCheckBox( "Autosave", "Enable Autosave", g_AutoSave_Enabled ); |
| 180 | ui::SpinButton autosave_frequency = page.appendSpinner( "Autosave Frequency (minutes)", m_AutoSave_Frequency, 1, 1, 60 ); |
| 181 | Widget_connectToggleDependency( autosave_frequency, autosave_enabled ); |
| 182 | page.appendCheckBox( "", "Save Snapshots", g_SnapShots_Enabled ); |
| 183 | } |
| 184 | void Autosave_constructPage( PreferenceGroup& group ){ |
| 185 | PreferencesPage page( group.createPage( "Autosave", "Autosave Preferences" ) ); |
| 186 | Autosave_constructPreferences( page ); |
| 187 | } |
| 188 | void Autosave_registerPreferencesPage(){ |
| 189 | PreferencesDialog_addSettingsPage(makeCallbackF(Autosave_constructPage)); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | #include "preferencesystem.h" |
| 194 | #include "stringio.h" |
| 195 | |
| 196 | void Autosave_Construct(){ |
| 197 | GlobalPreferenceSystem().registerPreference( "Autosave", make_property_string( g_AutoSave_Enabled ) ); |
| 198 | GlobalPreferenceSystem().registerPreference( "AutosaveMinutes", make_property_string( m_AutoSave_Frequency ) ); |
| 199 | GlobalPreferenceSystem().registerPreference( "Snapshots", make_property_string( g_SnapShots_Enabled ) ); |
| 200 | |
| 201 | Autosave_registerPreferencesPage(); |
| 202 | } |
| 203 | |
| 204 | void Autosave_Destroy(){ |
| 205 | } |