@@ -1979,6 +1979,42 @@ void MainWindow::buildMenu()
19791979 m_menuBuilder->addAction (path, action, 960 );
19801980 m_fileToolBar->addAction (action);
19811981 connect (action, SIGNAL (triggered ()), SLOT (saveFileAs ()));
1982+ // Initialize autosave feature
1983+ m_autosaveInterval = 5 ; // Autosave interval in minutes
1984+ m_autosaveTimer = new QTimer (this );
1985+ connect (m_autosaveTimer, &QTimer::timeout, this , &MainWindow::autosaveDocument);
1986+ m_autosaveTimer->start (m_autosaveInterval * 60000 ); // Convert minutes to milliseconds
1987+
1988+ void MainWindow::autosaveDocument ()
1989+ {
1990+ if (!m_molecule || !m_moleculeDirty) {
1991+ return ; // No molecule loaded or no changes made since the last save.
1992+ }
1993+
1994+ QString autosaveDirPath = QStandardPaths::writableLocation (QStandardPaths::AppDataLocation) + " /autosave" ;
1995+ QDir autosaveDir (autosaveDirPath);
1996+ if (!autosaveDir.exists ()) {
1997+ autosaveDir.mkpath (" ." );
1998+ }
1999+
2000+ // Construct autosave file name
2001+ QString autosaveFilename;
2002+ if (m_molecule->hasData (" fileName" )) {
2003+ QFileInfo fileInfo (m_molecule->data (" fileName" ).toString ().c_str ());
2004+ autosaveFilename = fileInfo.baseName () + " _autosave.cjson" ;
2005+ } else {
2006+ autosaveFilename = " unsaved_autosave.cjson" ;
2007+ }
2008+ QString autosaveFilePath = autosaveDirPath + " /" + autosaveFilename;
2009+
2010+ // Use CJSON format for autosaving
2011+ Io::CjsonFormat writer;
2012+ if (!writer.writeFile (autosaveFilePath, *m_molecule)) {
2013+ qWarning () << " Failed to autosave the document to" << autosaveFilePath;
2014+ } else {
2015+ qDebug () << " Document autosaved to" << autosaveFilePath;
2016+ }
2017+ }
19822018
19832019 // Export action for menu
19842020 QStringList exportPath = path;
@@ -2635,4 +2671,4 @@ bool MainWindow::handleCommand(const QString& command,
26352671 return false ;
26362672}
26372673
2638- } // End of Avogadro namespace
2674+ } // End of Avogadro namespace
0 commit comments