KTutorial  0.5.1
ScriptManager.cpp
00001 /***************************************************************************
00002  *   Copyright (C) 2009 by Daniel Calviño Sánchez <danxuliu@gmail.com>     *
00003  *   Copyright (C) 2010 by Daniel Calviño Sánchez <danxuliu@gmail.com>     *
00004  *   Copyright (C) 2012 by Daniel Calviño Sánchez <danxuliu@gmail.com>     *
00005  *                                                                         *
00006  *   This program is free software; you can redistribute it and/or modify  *
00007  *   it under the terms of the GNU General Public License as published by  *
00008  *   the Free Software Foundation; either version 2 of the License, or     *
00009  *   (at your option) any later version.                                   *
00010  *                                                                         *
00011  *   This program is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU General Public License for more details.                          *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU General Public License     *
00017  *   along with this program; If not, see <http://www.gnu.org/licenses/>.  *
00018  ***************************************************************************/
00019 
00020 #include "ScriptManager.h"
00021 
00022 #include <QDir>
00023 
00024 #include <KDebug>
00025 #include <KStandardDirs>
00026 
00027 #include "ScriptedTutorial.h"
00028 #include "../TutorialManager.h"
00029 
00030 namespace ktutorial {
00031 extern int debugArea();
00032 }
00033 
00034 namespace ktutorial {
00035 namespace scripting {
00036 
00037 //public:
00038 
00039 ScriptManager::ScriptManager() {
00040 }
00041 
00042 ScriptManager::~ScriptManager() {
00043 }
00044 
00045 void ScriptManager::loadTutorials(TutorialManager* tutorialManager) {
00046     Q_ASSERT(tutorialManager);
00047 
00048     QStringList baseDirectories = KGlobal::dirs()->resourceDirs("appdata");
00049     if (baseDirectories.count() == 0) {
00050         kWarning(debugArea()) << "No directories found for \"appdata\""
00051                               << "resource. Tutorials can't be loaded";
00052         return;
00053     }
00054 
00055     for (int i=0; i<baseDirectories.count(); ++i) {
00056         loadTutorialsFromDirectory(tutorialManager,
00057                                    baseDirectories[i] + "tutorials/");
00058     }
00059 }
00060 
00061 //private:
00062 
00063 void ScriptManager::loadTutorialsFromDirectory(TutorialManager* tutorialManager,
00064                                                const QString& directory) {
00065     QStringList entries = QDir(directory).entryList();
00066     for (int i=0; i<entries.count(); ++i) {
00067         QString fileName = directory + entries[i];
00068         ScriptedTutorial* scriptedTutorial = new ScriptedTutorial(fileName);
00069 
00070         if (scriptedTutorial->isValid()) {
00071             tutorialManager->registerTutorial(scriptedTutorial);
00072         } else {
00073             delete scriptedTutorial;
00074         }
00075     }
00076 }
00077 
00078 }
00079 }