diff -r e524edc45aff -r 3dabc6424d73 mapeditor.cpp --- a/mapeditor.cpp Wed Feb 27 16:09:06 2008 +0000 +++ b/mapeditor.cpp Wed Feb 27 16:09:06 2008 +0000 @@ -145,6 +145,10 @@ autosaveTimer=new QTimer (this); connect(autosaveTimer, SIGNAL(timeout()), this, SLOT(autosave())); + fileChangedTimer=new QTimer (this); + fileChangedTimer->start(3000); + connect(fileChangedTimer, SIGNAL(timeout()), this, SLOT(fileChanged())); + // Network netstate=Offline; @@ -185,6 +189,7 @@ { //cout <<"Destructor MapEditor\n"; autosaveTimer->stop(); + fileChangedTimer->stop(); // tmpMapDir is in tmpVymDir, so it gets removed automagically when vym closes @@ -1498,7 +1503,7 @@ { filePath=fpath; // becomes absolute path fileName=fpath; // gets stripped of path - destPath=destname; // needed for vymlinks + destPath=destname; // needed for vymlinks and during load to reset fileChangedTime // If fpath is not an absolute path, complete it filePath=QDir(fpath).absPath(); @@ -1543,7 +1548,8 @@ ErrorCode err=success; parseBaseHandler *handler; - switch (ftype) + fileType=ftype; + switch (fileType) { case VymMap: handler=new parseVYMHandler; break; case FreemindMap : handler=new parseFreemindHandler; break; @@ -1575,14 +1581,72 @@ QString("Add map %1 to %2").arg(fname).arg(getName(bo))); } - QFile file( fname ); + + // Create temporary directory for packing + bool ok; + QString tmpZipDir=makeTmpDir (ok,"vym-pack"); + if (!ok) + { + QMessageBox::critical( 0, tr( "Critical Load Error" ), + tr("Couldn't create temporary directory before load\n")); + return aborted; + } + + // Try to unzip file + err=unzipDir (tmpZipDir,fname); + QString xmlfile; + if (err==nozip) + { + xmlfile=fname; + zipped=false; + } else + { + zipped=true; + + // Look for mapname.xml + xmlfile= fname.left(fname.findRev(".",-1,true)); + xmlfile=xmlfile.section( '/', -1 ); + QFile mfile( tmpZipDir + "/" + xmlfile + ".xml"); + if (!mfile.exists() ) + { + // mapname.xml does not exist, well, + // maybe someone renamed the mapname.vym file... + // Try to find any .xml in the toplevel + // directory of the .vym file + QStringList flist=QDir (tmpZipDir).entryList("*.xml"); + if (flist.count()==1) + { + // Only one entry, take this one + xmlfile=tmpZipDir + "/"+flist.first(); + } else + { + for ( QStringList::Iterator it = flist.begin(); it != flist.end(); ++it ) + *it=tmpZipDir + "/" + *it; + // TODO Multiple entries, load all (but only the first one into this ME) + //mainWindow->fileLoadFromTmp (flist); + //returnCode=1; // Silently forget this attempt to load + qWarning ("MainWindow::load (fn) multimap found..."); + } + + if (flist.isEmpty() ) + { + QMessageBox::critical( 0, tr( "Critical Load Error" ), + tr("Couldn't find a map (*.xml) in .vym archive.\n")); + err=aborted; + } + } //file doesn't exist + else + xmlfile=mfile.name(); + } + + QFile file( xmlfile); // I am paranoid: file should exist anyway // according to check in mainwindow. if (!file.exists() ) { QMessageBox::critical( 0, tr( "Critical Parse Error" ), - tr("Couldn't open map " +fname)+"."); + tr(QString("Couldn't open map %1").arg(file.name()))); err=aborted; } else { @@ -1607,7 +1671,7 @@ file.close(); if ( ok ) { - model->reposition(); + model->reposition(); // FIXME reposition the view instead... xelection.update(); if (lmode==NewMap) { @@ -1616,6 +1680,9 @@ mapUnsaved=false; autosaveTimer->stop(); } + + // Reset timestamp to check for later updates of file + fileChangedTime=QFileInfo (destPath).lastModified(); } else { QMessageBox::critical( 0, tr( "Critical Parse Error" ), @@ -1625,22 +1692,102 @@ // partially read by the parser } } + + // Delete tmpDir + removeDir (QDir(tmpZipDir)); + updateActions(); return err; } -int MapEditor::save (const SaveMode &savemode) -{ +ErrorCode MapEditor::save (const SaveMode &savemode) +{ + cout <<"ME::save zipped="<0) return; if (mapUnsaved &&mapChanged && settings.value ("/mapeditor/autosave/use",true).toBool() ) - mainWindow->fileSave (this); + { + if (QFileInfo(filePath).lastModified()<=fileChangedTime) + mainWindow->fileSave (this); + else + if (debug) + cout <<" ME::autosave rejected, file on disk is newer than last save.\n"; + + } +} + +void MapEditor::fileChanged() +{ + // Check if file on disk has changed meanwhile + if (!filePath.isEmpty()) + { + QDateTime tmod=QFileInfo (filePath).lastModified(); + if (tmod>fileChangedTime) + { + + /* FIXME debug message, sometimes there's a glitch in the metrics... + cout << "ME::fileChanged()\n" + << " last saved: "<