@@ -37,7 +37,63 @@ void FileManager::setCurrentFileName(const QString fileName)
3737
3838void FileManager::newFile ()
3939{
40- // Logic to create a new file
40+ QString currentFileName = getCurrentFileName ();
41+ bool isFileSaved = !currentFileName.isEmpty ();
42+ bool isTextEditorEmpty = this ->m_editor ->toPlainText ().isEmpty ();
43+ // File has not been saved and the text editor is not empty
44+ if (!isFileSaved && !isTextEditorEmpty)
45+ {
46+ // Create box to prompt user to save changes to file
47+ QMessageBox promptBox;
48+ promptBox.setWindowTitle (" Save Current File" );
49+ promptBox.setText (" Would you like to save the file?" );
50+ promptBox.setStandardButtons (QMessageBox::Save | QMessageBox::Cancel);
51+ promptBox.setDefaultButton (QMessageBox::Save);
52+
53+ int option = promptBox.exec ();
54+ // return if the user hit Cancel button
55+ if (option == QMessageBox::Cancel)
56+ {
57+ return ;
58+ }
59+
60+ saveFile ();
61+ }
62+ // File has been previously saved
63+ else if (isFileSaved)
64+ {
65+ // Read from saved file and compare to current file
66+ QFile file (currentFileName);
67+ if (!file.open (QIODevice::ReadOnly | QIODevice::Text))
68+ return ;
69+ QTextStream in (&file);
70+ QString savedFileContents = in.readAll ();
71+ file.close ();
72+ if (savedFileContents != this ->m_editor ->toPlainText ().trimmed ())
73+ {
74+ // Create box to prompt user to save changes to file
75+ QMessageBox promptBox;
76+ promptBox.setWindowTitle (" Changes Detected" );
77+ promptBox.setText (" Would you like to save the current changes to the file?" );
78+ promptBox.setStandardButtons (QMessageBox::Save | QMessageBox::Cancel);
79+ promptBox.setDefaultButton (QMessageBox::Save);
80+ int option = promptBox.exec ();
81+ // return if the user hit Cancel button
82+ if (option == QMessageBox::Cancel)
83+ {
84+ return ;
85+ }
86+ saveFile ();
87+ }
88+ }
89+
90+ if (!m_currentFileName.isEmpty ())
91+ {
92+ setCurrentFileName (" " );
93+ m_editor->clear ();
94+ m_mainWindow->setWindowTitle (" Code Astra ~ untitled" );
95+ }
96+
4197}
4298
4399void FileManager::saveFile ()
@@ -235,14 +291,13 @@ OperationResult FileManager::deletePath(const QFileInfo &pathInfo)
235291 }
236292
237293 std::filesystem::path pathToDelete = pathInfo.absoluteFilePath ().toStdString ();
238-
239294 // Validate the input path
240295 if (!isValidPath (pathToDelete))
241296 {
242297 return {false , " ERROR: invalid file path." + pathToDelete.filename ().string ()};
243298 }
244-
245- if (!QFile::moveToTrash (pathToDelete ))
299+ QString qPathToDelete = QString::fromStdString (pathToDelete. string ());
300+ if (!QFile::moveToTrash (qPathToDelete ))
246301 {
247302 return {false , " ERROR: failed to delete: " + pathToDelete.string ()};
248303 }
@@ -349,4 +404,4 @@ OperationResult FileManager::duplicatePath(const QFileInfo &pathInfo)
349404 qDebug () << " Duplicated file to:" << QString::fromStdString (dupPath.string ());
350405
351406 return {true , dupPath.filename ().string ()};
352- }
407+ }
0 commit comments