# HG changeset patch
# User Periklis Ntanasis <pntanasis@gmail.com>
# Date 1372434901 -10800
# Node ID 33a30ce9782fab522f2cf0657094f7c25290688b
# Parent  3df990e2ee0e93a22379ffc11c63e7acab90a350
this seems to works, still wip

diff -r 3df990e2ee0e -r 33a30ce9782f QTfrontend/campaign.cpp
--- a/QTfrontend/campaign.cpp	Fri Jun 28 15:45:11 2013 +0300
+++ b/QTfrontend/campaign.cpp	Fri Jun 28 18:55:01 2013 +0300
@@ -18,9 +18,11 @@
 
 #include "campaign.h"
 #include "hwconsts.h"
+#include "DataManager.h"
 #include <QSettings>
 #include <QMap>
 #include <QDebug>
+#include <QObject>
 
 QStringList getCampMissionList(QString & campaign)
 {
@@ -75,6 +77,63 @@
 	return missionList;
 }
 
+QStringList getDescriptions(QString & campaignName, QString & teamName)
+{    
+    QStringList descriptionList;
+	QSettings teamfile(cfgdir->absolutePath() + "/Teams/" + teamName + ".hwt", QSettings::IniFormat, 0);
+    teamfile.setIniCodec("UTF-8");
+    unsigned int progress = teamfile.value("Campaign " + campaignName + "/Progress", 0).toInt();
+    qDebug("HERE is progress : %d",progress);
+    unsigned int unlockedMissions = teamfile.value("Campaign " + campaignName + "/UnlockedMissions", 0).toInt();
+    qDebug("HERE is unlocked missions : %d",unlockedMissions);
+    
+    QSettings campfile("physfs://Missions/Campaign/" + campaignName + "/campaign.ini", QSettings::IniFormat, 0);
+    campfile.setIniCodec("UTF-8");
+    unsigned int missionsNumber = campfile.value("MissionNum", 0).toInt();
+    qDebug("HERE is number of missions : %d",missionsNumber);  
+    
+    
+    DataManager & dataMgr = DataManager::instance();
+        // get locale
+        QSettings settings(dataMgr.settingsFileName(),
+                           QSettings::IniFormat);
+        QString loc = settings.value("misc/locale", "").toString();
+        if (loc.isEmpty())
+            loc = QLocale::system().name();
+        QString campaignDescFile = QString("physfs://Locale/campaigns_" + loc + ".txt");
+        // if file is non-existant try with language only
+        if (!QFile::exists(campaignDescFile))
+            campaignDescFile = QString("physfs://Locale/campaigns_" + loc.remove(QRegExp("_.*$")) + ".txt");
+
+        // fallback if file for current locale is non-existant
+        if (!QFile::exists(campaignDescFile))
+            campaignDescFile = QString("physfs://Locale/campaigns_en.txt");
+
+        QSettings m_info(campaignDescFile, QSettings::IniFormat, 0);
+        m_info.setIniCodec("UTF-8");
+    
+    if(progress>=0 and unlockedMissions==0)
+    {
+		for(unsigned int i=progress+1;i>0;i--)
+		{
+			//update descruiptions here
+			descriptionList += m_info.value(campaignName+"-"+ getCampaignMissionName(campaignName,i) + ".desc",
+                                            QObject::tr("No description available")).toString();
+		}
+	} 
+	else if(unlockedMissions>0)
+	{
+		qDebug("IN HERE !!!");  
+		for(unsigned int i=1;i<=unlockedMissions;i++)
+		{
+			QString missionNum = QString("%1").arg(i);
+			descriptionList += m_info.value(campaignName+"-"+ getCampaignMissionName(campaignName,i) + ".desc",
+                                            QObject::tr("No description available")).toString();
+		}
+	}
+	return descriptionList;
+}
+
 unsigned int getCampProgress(QString & teamName, QString & campName)
 {
     QSettings teamfile(cfgdir->absolutePath() + "/Teams/" + teamName + ".hwt", QSettings::IniFormat, 0);
diff -r 3df990e2ee0e -r 33a30ce9782f QTfrontend/campaign.h
--- a/QTfrontend/campaign.h	Fri Jun 28 15:45:11 2013 +0300
+++ b/QTfrontend/campaign.h	Fri Jun 28 18:55:01 2013 +0300
@@ -29,5 +29,6 @@
 QString getCampaignMissionName(QString campaign, unsigned int mNum);
 QMap<QString,QString> getUnlockedMissions2(QString & campaignName, QString & teamName);
 QStringList getCampMissionList2(QString & campaignName, QString & teamName);
+QStringList getDescriptions(QString & campaignName, QString & teamName);
 
 #endif
diff -r 3df990e2ee0e -r 33a30ce9782f QTfrontend/hwform.cpp
--- a/QTfrontend/hwform.cpp	Fri Jun 28 15:45:11 2013 +0300
+++ b/QTfrontend/hwform.cpp	Fri Jun 28 18:55:01 2013 +0300
@@ -1895,6 +1895,7 @@
 
 void HWForm::UpdateCampaignPage(int index)
 {
+	qDebug("UpdateCampaignPage");
     Q_UNUSED(index);
     HWTeam team(ui.pageCampaign->CBTeam->currentText());
     ui.pageCampaign->CBMission->clear();
@@ -1910,7 +1911,6 @@
     // this will be used later in UpdateCampaignPageMission() to update
     // the mission description in the campaign page
     bool updateMissionList = false;
-    QSettings * m_info;
     if(previousCampaignName.compare(campaignName)!=0 ||
             previousTeamName.compare(tName) != 0)
     {
@@ -1920,55 +1920,17 @@
         previousCampaignName = campaignName;
         previousTeamName = tName;
         updateMissionList = true;
-        // the following code was based on pagetraining.cpp
-        DataManager & dataMgr = DataManager::instance();
-        // get locale
-        QSettings settings(dataMgr.settingsFileName(),
-                           QSettings::IniFormat);
-        QString loc = settings.value("misc/locale", "").toString();
-        if (loc.isEmpty())
-            loc = QLocale::system().name();
-        QString campaignDescFile = QString("physfs://Locale/campaigns_" + loc + ".txt");
-        // if file is non-existant try with language only
-        if (!QFile::exists(campaignDescFile))
-            campaignDescFile = QString("physfs://Locale/campaigns_" + loc.remove(QRegExp("_.*$")) + ".txt");
-
-        // fallback if file for current locale is non-existant
-        if (!QFile::exists(campaignDescFile))
-            campaignDescFile = QString("physfs://Locale/campaigns_en.txt");
-
-        m_info = new QSettings(campaignDescFile, QSettings::IniFormat, this);
-        m_info->setIniCodec("UTF-8");
-        campaignMissionDescriptions.clear();
-        ui.pageCampaign->CBMission->clear();
     }
-/*
-    for (unsigned int i = qMin(m + 1, n); i > 0; i--)
-    {
-        if(updateMissionList)
-        {
-            campaignMissionDescriptions += m_info->value(campaignName+"-"+ getCampaignMissionName(campaignName,i) + ".desc",
-                                            tr("No description available")).toString();
-        }
-        ui.pageCampaign->CBMission->addItem(QString("Mission %1: ").arg(i) + QString(missionEntries[i-1]), QString(missionEntries[i-1]));
-    }*/
+    campaignMissionDescriptions = getDescriptions(campaignName,tName);
     for(int i=0;i<missionEntries.size();i++)
     {
-		/*if(updateMissionList)
-        {
-            campaignMissionDescriptions += m_info->value(campaignName+"-"+ getCampaignMissionName(campaignName,i) + ".desc",
-                                            tr("No description available")).toString();
-        }*/
         ui.pageCampaign->CBMission->addItem(QString("Mission %1: ").arg(missionEntries.size()-i) + QString(missionEntries[i]), QString(missionEntries[i]));
 	}
-    if(updateMissionList)
-        delete m_info;
-
-    UpdateCampaignPageMission(index);
 }
 
 void HWForm::UpdateCampaignPageMission(int index)
 {
+	qDebug("UpdateCampaignPageMission");
     // update thumbnail
     QString campaignName = ui.pageCampaign->CBCampaign->currentText();
     unsigned int mNum = ui.pageCampaign->CBMission->count() - ui.pageCampaign->CBMission->currentIndex();
@@ -1976,8 +1938,10 @@
     ui.pageCampaign->btnPreview->setIcon(QIcon((":/res/campaign/"+campaignName+"/"+image)));
     // update description
     // when campaign changes the UpdateCampaignPageMission is triggered with wrong values
-    // this will cause segfault. This check prevents illegal memory reads
+    // this will cause segfault. This check prevents illegal memory reads    
+    qDebug("INDEX IS %d and number of descs is %d",index,campaignMissionDescriptions.count());
     if(index > -1 && index < campaignMissionDescriptions.count()) {
+		qDebug("INSIDE IF *******");
         ui.pageCampaign->lbltitle->setText("<h2>"+ui.pageCampaign->CBMission->currentText()+"</h2>");
         ui.pageCampaign->lbldescription->setText(campaignMissionDescriptions[index]);
     }
diff -r 3df990e2ee0e -r 33a30ce9782f share/hedgewars/Data/Locale/campaigns_en.txt
--- a/share/hedgewars/Data/Locale/campaigns_en.txt	Fri Jun 28 15:45:11 2013 +0300
+++ b/share/hedgewars/Data/Locale/campaigns_en.txt	Fri Jun 28 18:55:01 2013 +0300
@@ -19,3 +19,5 @@
 A Classic Fairytale-epil.desc="Congratulations! Leaks a lot can finally leave in peace and get praised by his new friends and his tribe. Be proud for what you succeed! You can play again previous missions and see the other possible endings."
 
 A Space Adventure-cosmos.desc="Hogera, the planet of hogs is about to be hit by a gigantic meteorite. In this race for survival you have to lead PAoTH's best pilot, Hog Solo, in a space trip around the neighbor planets to collect all the 4 pieces of the long lost anti gravity device!"
+A Space Adventure-moon01.desc="MOON TEST"
+A Space Adventure-ice.desc="ICE TEST"
diff -r 3df990e2ee0e -r 33a30ce9782f share/hedgewars/Data/Missions/Campaign/A Space Adventure/campaign.ini
--- a/share/hedgewars/Data/Missions/Campaign/A Space Adventure/campaign.ini	Fri Jun 28 15:45:11 2013 +0300
+++ b/share/hedgewars/Data/Missions/Campaign/A Space Adventure/campaign.ini	Fri Jun 28 18:55:01 2013 +0300
@@ -1,4 +1,4 @@
-MissionNum=1
+MissionNum=5
 ResetRetry=1
 
 [Mission 1]