615
QString LandscapeMgr::installLandscapeFromArchive(QString sourceFilePath, bool display, bool toMainDirectory)
617
if (!QFile::exists(sourceFilePath))
619
qDebug() << "LandscapeMgr: File does not exist:" << sourceFilePath;
623
QDir parentDestinationDir;
624
//TODO: Fix the "for all users" option
625
parentDestinationDir.setPath(StelFileMgr::getUserDir());
627
if (!parentDestinationDir.exists("landscapes"))
629
//qDebug() << "LandscapeMgr: No 'landscapes' subdirectory exists in" << parentDestinationDir.absolutePath();
630
if (!parentDestinationDir.mkdir("landscapes"))
632
qWarning() << "LandscapeMgr: Unable to install landscape: Unable to create sub-directory 'landscapes' in" << parentDestinationDir.absolutePath();
636
QDir destinationDir (parentDestinationDir.absoluteFilePath("landscapes"));
638
KZip sourceArchive(sourceFilePath);
639
if(!sourceArchive.open(QIODevice::ReadOnly))
641
qWarning() << "LandscapeMgr: Unable to open source archive file:" << sourceFilePath;
645
//Detect top directory
646
const KArchiveDirectory * archiveTopDirectory = NULL;
647
QStringList topLevelContents = sourceArchive.directory()->entries();
648
foreach (QString entryPath, topLevelContents)
650
if (sourceArchive.directory()->entry(entryPath)->isDirectory())
652
if((dynamic_cast<const KArchiveDirectory*>(sourceArchive.directory()->entry(entryPath)))->entries().contains("landscape.ini"))
654
archiveTopDirectory = dynamic_cast<const KArchiveDirectory*>(sourceArchive.directory()->entry(entryPath));
659
if (archiveTopDirectory == NULL)
661
qWarning() << "LandscapeMgr: Unable to install landscape. There is no directory that contains a 'landscape.ini' file in the source archive.";
666
qDebug() << "LandscapeMgr: Contents of the source archive:" << endl
667
<< "- top level direcotory:" << archiveTopDirectory->name() << endl
668
<< "- contents:" << archiveTopDirectory->entries();
671
//Check if the top directory name is unique
672
//TODO: Prompt rename? Rename silently?
674
if (destinationDir.exists(archiveTopDirectory->name()))
676
qWarning() << "LandscapeMgr: Unable to install landscape. A directory named" << archiveTopDirectory->name() << "already exists in" << destinationDir.absolutePath();
680
QString landscapeID = archiveTopDirectory->name();
681
if (getAllLandscapeIDs().contains(landscapeID))
683
qWarning() << "LandscapeMgr: Unable to install landscape. A directory named" << landscapeID << "already exists.";
687
//Read the .ini file and check if the landscape name is unique
688
QTemporaryFile tempLandscapeIni("landscapeXXXXXX.ini");
689
if (tempLandscapeIni.open())
691
const KZipFileEntry * archLandscapeIni = static_cast<const KZipFileEntry*>(archiveTopDirectory->entry("landscape.ini"));
692
tempLandscapeIni.write(archLandscapeIni->createDevice()->readAll());
693
tempLandscapeIni.close();
695
QSettings confLandscapeIni(tempLandscapeIni.fileName(), StelIniFormat);
696
QString landscapeName = confLandscapeIni.value("landscape/name").toString();
697
if (getAllLandscapeNames().contains(landscapeName))
699
qWarning() << "LandscapeMgr: Unable to install landscape. There is already a landscape named" << landscapeName;
704
//Copy the landscape directory to the target
705
//sourceArchive.directory()->copyTo(destinationDir.absolutePath());
706
if(!destinationDir.mkdir(landscapeID))
708
qWarning() << "LandscapeMgr: Unable to install landscape. Unable to create" << landscapeID << "directory in" << destinationDir.absolutePath();
711
destinationDir.cd(landscapeID);
712
QString destinationDirPath = destinationDir.absolutePath();
713
QStringList landscapeFileEntries = archiveTopDirectory->entries();
714
foreach (QString entry, landscapeFileEntries)
716
const KArchiveEntry * archEntry = archiveTopDirectory->entry(entry);
717
if(archEntry->isFile())
719
static_cast<const KZipFileEntry*>(archEntry)->copyTo(destinationDirPath);
723
sourceArchive.close();
725
//If necessary, make the new landscape the current landscape
728
setCurrentLandscapeID(landscapeID);
731
//Make sure that everyone knows that the list of available landscapes has changed
732
emit landscapesChanged();
734
qDebug() << "LandscapeMgr: Successfully installed landscape directory" << landscapeID << "to" << destinationDir.absolutePath();
738
bool LandscapeMgr::removeLandscape(QString landscapeID)
740
if (landscapeID.isEmpty())
742
qWarning() << "LandscapeMgr: Error! No landscape ID passed to removeLandscape().";
746
qDebug() << "LandscapeMgr: Trying to remove landscape" << landscapeID;
748
QString landscapePath = getLandscapePath(landscapeID);
749
if (landscapePath.isEmpty())
752
QDir landscapeDir(landscapePath);
753
foreach (QString fileName, landscapeDir.entryList(QDir::Files | QDir::NoDotAndDotDot))
755
if(!landscapeDir.remove(fileName))
757
qDebug() << "LandscapeMgr: Unable to remove" << fileName;
762
if(!landscapeDir.rmdir(landscapeID))
764
qWarning() << "LandscapeMgr: Error! Landscape" << landscapeID << "could not be removed. Some files were deleted, but not all.";
768
qDebug() << "LandscapeMgr: Successfully removed" << landscapePath;
770
//If the landscape has been selected, revert to the default one
771
//TODO: Make this optional?
772
if (getCurrentLandscapeID() == landscapeID)
774
if(getDefaultLandscapeID() == landscapeID)
776
setDefaultLandscapeID("guereins");
777
//TODO: Find what happens if a missing landscape is specified in the configuration file
778
//TODO: Think of a way to specify centrally a default default landscape
781
setCurrentLandscapeID(getDefaultLandscapeID());
784
//Make sure that everyone knows that the list of available landscapes has changed
785
emit landscapesChanged();
790
QString LandscapeMgr::getLandscapePath(QString landscapeID)
793
//Is this necessary? This function is private.
794
if (landscapeID.isEmpty())
799
result = StelFileMgr::findFile("landscapes/" + landscapeID, StelFileMgr::Directory);
801
catch (std::runtime_error &e)
803
qWarning() << "LandscapeMgr: Error! Unable to find" << landscapeID << ":" << e.what();
810
QString LandscapeMgr::loadLandscapeName(QString landscapeID)
812
QString landscapeName;
813
if (landscapeID.isEmpty())
815
qWarning() << "LandscapeMgr: Error! No landscape ID passed to loadLandscapeName().";
816
return landscapeName;
819
QString landscapePath = getLandscapePath(landscapeID);
820
if (landscapePath.isEmpty())
821
return landscapeName;
823
QDir landscapeDir(landscapePath);
824
if (landscapeDir.exists("landscape.ini"))
826
QString landscapeSettingsPath = landscapeDir.filePath("landscape.ini");
827
QSettings landscapeSettings(landscapeSettingsPath, StelIniFormat);
828
landscapeName = landscapeSettings.value("landscape/name").toString();
832
qWarning() << "LandscapeMgr: Error! Landscape directory" << landscapePath << "does not contain a 'landscape.ini' file";
835
return landscapeName;
838
quint64 LandscapeMgr::loadLandscapeSize(QString landscapeID)
840
quint64 landscapeSize = 0;
841
if (landscapeID.isEmpty())
843
qWarning() << "LandscapeMgr: Error! No landscape ID passed to loadLandscapeSize().";
844
return landscapeSize;
847
QString landscapePath = getLandscapePath(landscapeID);
848
if (landscapePath.isEmpty())
849
return landscapeSize;
851
QDir landscapeDir(landscapePath);
852
foreach (QFileInfo file, landscapeDir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot))
854
//qDebug() << "name:" << file.baseName() << "size:" << file.size();
855
landscapeSize += file.size();
858
return landscapeSize;