aboutsummaryrefslogtreecommitdiff
path: root/mp/src/vgui2/vgui_controls/FileOpenStateMachine.cpp
blob: c611c11d48dbd1675dc41348d49c704bf619809e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// This is a helper class designed to help with the chains of modal dialogs
// encountered when trying to open or save a particular file
//
//=============================================================================

#include "vgui_controls/FileOpenStateMachine.h"
#include "tier1/KeyValues.h"
#include "vgui_controls/FileOpenDialog.h"
#include "vgui_controls/MessageBox.h"
#include "vgui_controls/perforcefilelistframe.h"
#include "vgui_controls/savedocumentquery.h"
#include "filesystem.h"
#include "p4lib/ip4.h"
#include "tier2/tier2.h"
#include "tier0/icommandline.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"


using namespace vgui;


//-----------------------------------------------------------------------------
// Constructor
//-----------------------------------------------------------------------------
FileOpenStateMachine::FileOpenStateMachine( vgui::Panel *pParent, IFileOpenStateMachineClient *pClient ) : BaseClass( pParent, "FileOpenStateMachine" )
{
	m_pClient = pClient;
	m_CompletionState = SUCCESSFUL;
	m_CurrentState = STATE_NONE;
	m_pContextKeyValues = NULL;
	SetVisible( false );
}

FileOpenStateMachine::~FileOpenStateMachine()
{
	CleanUpContextKeyValues();
}


//-----------------------------------------------------------------------------
// Cleans up keyvalues
//-----------------------------------------------------------------------------
void FileOpenStateMachine::CleanUpContextKeyValues()
{
	if ( m_pContextKeyValues )
	{
		m_pContextKeyValues->deleteThis();
		m_pContextKeyValues = NULL;
	}
}


//-----------------------------------------------------------------------------
// Returns the state machine completion state
//-----------------------------------------------------------------------------
FileOpenStateMachine::CompletionState_t FileOpenStateMachine::GetCompletionState()
{
	return m_CompletionState;
}


//-----------------------------------------------------------------------------
// Utility to set the completion state
//-----------------------------------------------------------------------------
void FileOpenStateMachine::SetCompletionState( FileOpenStateMachine::CompletionState_t state )
{
	m_CompletionState = state;
	if ( m_CompletionState == IN_PROGRESS )
		return;

	m_CurrentState = STATE_NONE;

	KeyValues *kv = new KeyValues( "FileStateMachineFinished" );
	kv->SetInt( "completionState", m_CompletionState );
	kv->SetInt( "wroteFile", m_bWroteFile );
	kv->SetString( "fullPath", m_FileName.Get() );
	kv->SetString( "fileType", m_bIsOpeningFile ? m_OpenFileType.Get() : m_SaveFileType.Get() );
	if ( m_pContextKeyValues )
	{
		kv->AddSubKey( m_pContextKeyValues );
		m_pContextKeyValues = NULL;
	}
	PostActionSignal( kv );
}


//-----------------------------------------------------------------------------
// Called by the message box in OverwriteFileDialog
//-----------------------------------------------------------------------------
void FileOpenStateMachine::OnOverwriteFile( )
{
	CheckOutDialog( );
}

void FileOpenStateMachine::OnCancelOverwriteFile( )
{
	SetCompletionState( FILE_NOT_OVERWRITTEN );
}


//-----------------------------------------------------------------------------
// Shows the overwrite existing file dialog
//-----------------------------------------------------------------------------
void FileOpenStateMachine::OverwriteFileDialog( )
{
	if ( !g_pFullFileSystem->FileExists( m_FileName ) )
	{
		CheckOutDialog( );
		return;
	}

	m_CurrentState = STATE_SHOWING_OVERWRITE_DIALOG;

	char pBuf[1024];
	Q_snprintf( pBuf, sizeof(pBuf), "File already exists. Overwrite it?\n\n\"%s\"\n", m_FileName.Get() ); 
	vgui::MessageBox *pMessageBox = new vgui::MessageBox( "Overwrite Existing File?", pBuf, GetParent() );
	pMessageBox->AddActionSignalTarget( this );
	pMessageBox->SetOKButtonVisible( true );
	pMessageBox->SetOKButtonText( "Yes" );
	pMessageBox->SetCancelButtonVisible( true );
	pMessageBox->SetCancelButtonText( "No" );
	pMessageBox->SetCloseButtonVisible( false ); 
	pMessageBox->SetCommand( new KeyValues( "OverwriteFile" ) );
	pMessageBox->SetCancelCommand( new KeyValues( "CancelOverwriteFile" ) );
	pMessageBox->DoModal();
}


//-----------------------------------------------------------------------------
// Used to open a particular file in perforce, and deal with all the lovely dialogs
//-----------------------------------------------------------------------------
void FileOpenStateMachine::OnFileSelectionCancelled()
{
	if ( m_CurrentState == STATE_SHOWING_SAVE_DIALOG )
	{
		SetCompletionState( FILE_SAVE_NAME_NOT_SPECIFIED );
		return;
	}

	if ( m_CurrentState == STATE_SHOWING_OPEN_DIALOG )
	{
		SetCompletionState( FILE_OPEN_NAME_NOT_SPECIFIED );
		return;
	}

	Assert(0);
}


//-----------------------------------------------------------------------------
// Used to open a particular file in perforce, and deal with all the lovely dialogs
//-----------------------------------------------------------------------------
void FileOpenStateMachine::OnFileSelected( KeyValues *pKeyValues )
{
	if ( m_CurrentState == STATE_SHOWING_SAVE_DIALOG )
	{
		m_FileName = pKeyValues->GetString( "fullpath" );
		const char *pFilterInfo = pKeyValues->GetString( "filterinfo" );
		if ( pFilterInfo )
		{
			m_SaveFileType = pFilterInfo;
		}
		OverwriteFileDialog();
		return;
	}

	if ( m_CurrentState == STATE_SHOWING_OPEN_DIALOG )
	{
		m_FileName = pKeyValues->GetString( "fullpath" );
		const char *pFilterInfo = pKeyValues->GetString( "filterinfo" );
		if ( pFilterInfo )
		{
			m_OpenFileType = pFilterInfo;
		}
		ReadFile( );
		return;
	}

	Assert(0);
}


//-----------------------------------------------------------------------------
// Writes the file out
//-----------------------------------------------------------------------------
void FileOpenStateMachine::WriteFile()
{
	m_CurrentState = STATE_WRITING_FILE;
	if ( !m_pClient->OnWriteFileToDisk( m_FileName, m_SaveFileType, m_pContextKeyValues ) )
	{
		SetCompletionState( ERROR_WRITING_FILE );
		return;
	}

	m_bWroteFile = true;
	if ( m_bShowPerforceDialogs )
	{
		m_CurrentState = STATE_SHOWING_PERFORCE_ADD_DIALOG;
		ShowPerforceQuery( GetParent(), m_FileName, this, NULL, PERFORCE_ACTION_FILE_ADD );
		return;
	}

	if ( !m_bIsOpeningFile )
	{
		SetCompletionState( SUCCESSFUL );
		return;
	}

	OpenFileDialog();
}


//-----------------------------------------------------------------------------
// Called by the message box in MakeFileWriteableDialog
//-----------------------------------------------------------------------------
void FileOpenStateMachine::OnMakeFileWriteable( )
{
	if ( !g_pFullFileSystem->SetFileWritable( m_FileName, true ) )
	{
		SetCompletionState( ERROR_MAKING_FILE_WRITEABLE );
		return;
	}

	WriteFile();
}

void FileOpenStateMachine::OnCancelMakeFileWriteable( )
{
	SetCompletionState( FILE_NOT_MADE_WRITEABLE );
}


//-----------------------------------------------------------------------------
// Shows the make file writeable dialog
//-----------------------------------------------------------------------------
void FileOpenStateMachine::MakeFileWriteableDialog( )
{
	// If the file is writeable, write it!
	if ( !g_pFullFileSystem->FileExists( m_FileName ) || g_pFullFileSystem->IsFileWritable( m_FileName ) )
	{
		WriteFile();
		return;
	}

	// If it's in perforce, and not checked out, then we must abort.
	bool bIsInPerforce = p4->IsFileInPerforce( m_FileName );
	bool bIsOpened = ( p4->GetFileState( m_FileName ) != P4FILE_UNOPENED );
	if ( bIsInPerforce && !bIsOpened )
	{
		SetCompletionState( FILE_NOT_CHECKED_OUT );
		return;
	}

	m_CurrentState = STATE_SHOWING_MAKE_FILE_WRITEABLE_DIALOG;

	char pBuf[1024];
	Q_snprintf( pBuf, sizeof(pBuf), "Encountered read-only file. Should it be made writeable?\n\n\"%s\"\n", m_FileName.Get() ); 
	vgui::MessageBox *pMessageBox = new vgui::MessageBox( "Make File Writeable?", pBuf, GetParent() );
	pMessageBox->AddActionSignalTarget( this );
	pMessageBox->SetOKButtonVisible( true );
	pMessageBox->SetOKButtonText( "Yes" );
	pMessageBox->SetCancelButtonVisible( true );
	pMessageBox->SetCancelButtonText( "No" );
	pMessageBox->SetCloseButtonVisible( false ); 
	pMessageBox->SetCommand( new KeyValues( "MakeFileWriteable" ) );
	pMessageBox->SetCancelCommand( new KeyValues( "CancelMakeFileWriteable" ) );
	pMessageBox->DoModal();
}


//-----------------------------------------------------------------------------
// Called when ShowPerforceQuery completes
//-----------------------------------------------------------------------------
void FileOpenStateMachine::OnPerforceQueryCompleted( KeyValues *pKeyValues )
{
	if ( m_CurrentState == STATE_SHOWING_CHECK_OUT_DIALOG )
	{
		if ( pKeyValues->GetInt( "operationPerformed" ) == 0 )
		{
			SetCompletionState( FILE_NOT_CHECKED_OUT );
			return;
		}

		MakeFileWriteableDialog();
		return;
	}

	if ( m_CurrentState == STATE_SHOWING_PERFORCE_ADD_DIALOG )
	{
		if ( !m_bIsOpeningFile )
		{
			SetCompletionState( SUCCESSFUL );
			return;
		}

		OpenFileDialog();
		return;
	}

	Assert(0);
}


//-----------------------------------------------------------------------------
// Used to open a particular file in perforce, and deal with all the lovely dialogs
//-----------------------------------------------------------------------------
void FileOpenStateMachine::CheckOutDialog( )
{
	if ( m_bShowPerforceDialogs )
	{
		m_CurrentState = STATE_SHOWING_CHECK_OUT_DIALOG;
		ShowPerforceQuery( GetParent(), m_FileName, this, NULL, PERFORCE_ACTION_FILE_EDIT );
		return;
	}

	WriteFile();
}


//-----------------------------------------------------------------------------
// These 3 messages come from the savedocumentquery dialog
//-----------------------------------------------------------------------------
void FileOpenStateMachine::OnSaveFile()
{
	if ( !m_FileName[0] || !Q_IsAbsolutePath( m_FileName ) )
	{
		m_CurrentState = STATE_SHOWING_SAVE_DIALOG;

		FileOpenDialog *pDialog = new FileOpenDialog( GetParent(), "Save As", false );
		m_pClient->SetupFileOpenDialog( pDialog, false, m_SaveFileType, m_pContextKeyValues );
		pDialog->SetDeleteSelfOnClose( true );
		pDialog->AddActionSignalTarget( this );
		pDialog->DoModal( );
		return;
	}

	CheckOutDialog( );
}

void FileOpenStateMachine::OnMarkNotDirty()
{
	if ( !m_bIsOpeningFile )
	{
		SetCompletionState( SUCCESSFUL );
		return;
	}

	// Jump right to opening the file
	OpenFileDialog( );
}

void FileOpenStateMachine::OnCancelSaveDocument()
{
	SetCompletionState( FILE_SAVE_CANCELLED );
}


//-----------------------------------------------------------------------------
// Show the save document query dialog
//-----------------------------------------------------------------------------
void FileOpenStateMachine::ShowSaveQuery( )
{
	m_CurrentState = STATE_SHOWING_SAVE_DIRTY_FILE_DIALOG;
	ShowSaveDocumentQuery( GetParent(), m_FileName, m_SaveFileType, 0, this, NULL );
}


//-----------------------------------------------------------------------------
// Used to save a specified file, and deal with all the lovely dialogs
//-----------------------------------------------------------------------------
void FileOpenStateMachine::SaveFile( KeyValues *pContextKeyValues, const char *pFileName, const char *pFileType, int nFlags )
{
	CleanUpContextKeyValues();
	SetCompletionState( IN_PROGRESS );
	m_pContextKeyValues = pContextKeyValues;
	m_FileName = pFileName;
	m_SaveFileType = pFileType;
	m_OpenFileType = NULL;
	m_OpenFileName = NULL;

	// Clear the P4 dialog flag for SDK users and licensees without Perforce
	if ( CommandLine()->FindParm( "-nop4" ) )
	{
		nFlags &= ~FOSM_SHOW_PERFORCE_DIALOGS;
	}

	m_bShowPerforceDialogs = ( nFlags & FOSM_SHOW_PERFORCE_DIALOGS ) != 0;
	m_bShowSaveQuery = ( nFlags & FOSM_SHOW_SAVE_QUERY ) != 0;
	m_bIsOpeningFile = false;
	m_bWroteFile = false;

	if ( m_bShowSaveQuery )
	{
		ShowSaveQuery();
		return;
	}

	OnSaveFile();
}


//-----------------------------------------------------------------------------
// Reads the file in
//-----------------------------------------------------------------------------
void FileOpenStateMachine::ReadFile()
{
	m_CurrentState = STATE_READING_FILE;
	if ( !m_pClient->OnReadFileFromDisk( m_FileName, m_OpenFileType, m_pContextKeyValues ) )
	{
		SetCompletionState( ERROR_READING_FILE );
		return;
	}

	SetCompletionState( SUCCESSFUL );
}


//-----------------------------------------------------------------------------
// Shows the open file dialog
//-----------------------------------------------------------------------------
void FileOpenStateMachine::OpenFileDialog( )
{
	m_CurrentState = STATE_SHOWING_OPEN_DIALOG;

	if ( m_OpenFileName.IsEmpty() )
	{
		FileOpenDialog *pDialog = new FileOpenDialog( GetParent(), "Open", true );
		m_pClient->SetupFileOpenDialog( pDialog, true, m_OpenFileType, m_pContextKeyValues );
		pDialog->SetDeleteSelfOnClose( true );
		pDialog->AddActionSignalTarget( this );
		pDialog->DoModal( );
	}
	else
	{
		m_FileName = m_OpenFileName;
		ReadFile();
	}
}


//-----------------------------------------------------------------------------
// Opens a file, saves an existing one if necessary
//-----------------------------------------------------------------------------
void FileOpenStateMachine::OpenFile( const char *pOpenFileType, KeyValues *pContextKeyValues, const char *pSaveFileName, const char *pSaveFileType, int nFlags )
{
	CleanUpContextKeyValues();
	SetCompletionState( IN_PROGRESS );
	m_pContextKeyValues = pContextKeyValues;
	m_FileName = pSaveFileName;
	m_SaveFileType = pSaveFileType;
	m_OpenFileType = pOpenFileType;
	m_OpenFileName = NULL;
	m_bShowPerforceDialogs = ( nFlags & FOSM_SHOW_PERFORCE_DIALOGS ) != 0;
	m_bShowSaveQuery = ( nFlags & FOSM_SHOW_SAVE_QUERY ) != 0;
	m_bIsOpeningFile = true;
	m_bWroteFile = false;

	if ( m_bShowSaveQuery )
	{
		ShowSaveQuery();
		return;
	}

	OpenFileDialog();
}


//-----------------------------------------------------------------------------
// Version of OpenFile that skips browsing for a particular file to open
//-----------------------------------------------------------------------------
void FileOpenStateMachine::OpenFile( const char *pOpenFileName, const char *pOpenFileType, KeyValues *pContextKeyValues, const char *pSaveFileName, const char *pSaveFileType, int nFlags )
{
	CleanUpContextKeyValues();
	SetCompletionState( IN_PROGRESS );
	m_pContextKeyValues = pContextKeyValues;
	m_FileName = pSaveFileName;
	m_SaveFileType = pSaveFileType;
	m_OpenFileType = pOpenFileType;
	m_bShowPerforceDialogs = ( nFlags & FOSM_SHOW_PERFORCE_DIALOGS ) != 0;
	m_bShowSaveQuery = ( nFlags & FOSM_SHOW_SAVE_QUERY ) != 0;
	m_bIsOpeningFile = true;
	m_bWroteFile = false;
	m_OpenFileName = pOpenFileName;
	if ( m_bShowSaveQuery )
	{
		ShowSaveQuery();
		return;
	}

	OpenFileDialog();
}