1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2019 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program 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 Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 // create_file_dlg.cpp : implementation file
24 #include "object_viewer.h"
25 #include "create_file_dlg.h"
29 /////////////////////////////////////////////////////////////////////////////
30 // CCreateFileDlg dialog
33 CCreateFileDlg::CCreateFileDlg(const CString
&title
, const std::string
&defaultBasePath
, const std::string
&extension
, CWnd
* pParent
/*=NULL*/)
34 : CDialog(CCreateFileDlg::IDD
, pParent
)
36 //{{AFX_DATA_INIT(CCreateFileDlg)
37 // NOTE: the ClassWizard will add member initialization here
40 _Extension
= extension
;
41 _DefaultBasePath
= defaultBasePath
;
45 void CCreateFileDlg::DoDataExchange(CDataExchange
* pDX
)
47 CDialog::DoDataExchange(pDX
);
48 //{{AFX_DATA_MAP(CCreateFileDlg)
49 // NOTE: the ClassWizard will add DDX and DDV calls here
54 BEGIN_MESSAGE_MAP(CCreateFileDlg
, CDialog
)
55 //{{AFX_MSG_MAP(CCreateFileDlg)
56 ON_BN_CLICKED(IDC_BROWSE
, OnBrowse
)
60 /////////////////////////////////////////////////////////////////////////////
61 // CCreateFileDlg message handlers
63 void CCreateFileDlg::OnBrowse()
66 if (browseFolder(getStrRsc(IDS_CHOOSE_BASE_PATH
), chosenPath
, this->m_hWnd
))
68 GetDlgItem(IDC_LOCATION
)->SetWindowText((LPCTSTR
) chosenPath
);
73 //*************************************************************************************************
74 BOOL
CCreateFileDlg::OnInitDialog()
76 CDialog::OnInitDialog();
77 SetWindowText((LPCTSTR
) _Title
);
78 GetDlgItem(IDC_LOCATION
)->SetWindowText(nlUtf8ToTStr(_DefaultBasePath
));
79 if (!_DefaultBasePath
.empty())
81 GetDlgItem(IDC_FILENAME
)->SetFocus();
85 GetDlgItem(IDC_LOCATION
)->SetFocus();
87 return TRUE
; // return TRUE unless you set the focus to a control
88 // EXCEPTION: OCX Property Pages should return FALSE
91 //*************************************************************************************************
92 void CCreateFileDlg::OnOK()
95 GetDlgItem(IDC_FILENAME
)->GetWindowText(filename
);
96 _Filename
= NLMISC::tStrToUtf8(filename
);
98 GetDlgItem(IDC_LOCATION
)->GetWindowText(location
);
99 _Path
= NLMISC::tStrToUtf8(location
);
102 localizedMessageBox(*this, IDS_EMPTY_PATH
, IDS_ERROR
, MB_ICONEXCLAMATION
);
105 if (_Filename
.empty())
107 localizedMessageBox(*this, IDS_FILENAME_PATH
, IDS_ERROR
, MB_ICONEXCLAMATION
);
110 // check that filename is valid (should be a single file name)
111 if (_Filename
!= NLMISC::CFile::getFilename(_Filename
))
113 localizedMessageBox(*this, IDS_INVALID_FILENAME
, IDS_ERROR
, MB_ICONEXCLAMATION
);
116 // attempt to create containing folder
117 if (!NLMISC::CFile::isExists(_Path
))
119 bool result
= NLMISC::CFile::createDirectory(_Path
);
122 MessageBox((LPCTSTR
) (getStrRsc(IDS_COULDNT_CREATE_DIRECTORY
) + _Path
.c_str()), getStrRsc(IDS_ERROR
), MB_ICONEXCLAMATION
);
126 std::string oldPath
= NLMISC::CPath::getCurrentPath();
127 if (!NLMISC::CPath::setCurrentPath(_Path
.c_str()))
129 MessageBox((LPCTSTR
) (getStrRsc(IDS_COULDNT_CREATE_DIRECTORY
) + _Path
.c_str()), getStrRsc(IDS_ERROR
), MB_ICONEXCLAMATION
);
132 _FullPath
= NLMISC::CPath::getFullPath(_Filename
, false);
133 NLMISC::CPath::setCurrentPath(oldPath
.c_str());
134 // append extension if not present
135 if (NLMISC::nlstricmp(NLMISC::CFile::getExtension(_Filename
), _Extension
) != 0)
137 _Filename
+= "." + _Extension
;
138 _FullPath
+= "." + _Extension
;
143 void CCreateFileDlg::OnCancel()
151 //*************************************************************************************************
152 bool CCreateFileDlg::touchFile()
154 std::string path
= getPath();
155 std::string filename
= getFileName();
156 std::string fullPath
= getFullPath();
157 // check if file already exists
158 if (NLMISC::CFile::isExists(fullPath
))
160 int result
= MessageBox((LPCTSTR
) (CString(filename
.c_str()) + getStrRsc(IDS_OVERWRITE_FILE
)), getStrRsc(IDS_WARNING
), MB_ICONEXCLAMATION
);
161 if (result
!= IDOK
) return false;
163 // create a dummy file
164 NLMISC::COFile testFile
;
165 if (!testFile
.open(fullPath
))
167 localizedMessageBox(*this, IDS_CANNOT_CREATE_FILE
, IDS_ERROR
, MB_ICONEXCLAMATION
);