QTfrontend/gameconfig.cpp
changeset 85 44d9045b26ff
parent 84 0f6669da2fcb
child 86 664b536a1c27
equal deleted inserted replaced
84:0f6669da2fcb 85:44d9045b26ff
     1 /*
       
     2  * Hedgewars, a worms-like game
       
     3  * Copyright (c) 2006 Andrey Korotaev <unC0Rr@gmail.com>
       
     4  *
       
     5  * Distributed under the terms of the BSD-modified licence:
       
     6  *
       
     7  * Permission is hereby granted, free of charge, to any person obtaining a copy
       
     8  * of this software and associated documentation files (the "Software"), to deal
       
     9  * with the Software without restriction, including without limitation the
       
    10  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
       
    11  * sell copies of the Software, and to permit persons to whom the Software is
       
    12  * furnished to do so, subject to the following conditions:
       
    13  *
       
    14  * 1. Redistributions of source code must retain the above copyright notice,
       
    15  *    this list of conditions and the following disclaimer.
       
    16  * 2. Redistributions in binary form must reproduce the above copyright notice,
       
    17  *    this list of conditions and the following disclaimer in the documentation
       
    18  *    and/or other materials provided with the distribution.
       
    19  * 3. The name of the author may not be used to endorse or promote products
       
    20  *    derived from this software without specific prior written permission.
       
    21  *
       
    22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
       
    23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
       
    24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
       
    25  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
       
    27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
       
    28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
       
    29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
       
    30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
       
    31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    32  */
       
    33 
       
    34 #include <QMessageBox>
       
    35 #include <QTextStream>
       
    36 #include "gameconfig.h"
       
    37 
       
    38 GameConfig::GameConfig(HWForm * FormWidgets)
       
    39 	: QObject()
       
    40 {
       
    41 	Form = FormWidgets;
       
    42 
       
    43 	cfgdir.setPath(cfgdir.homePath());
       
    44 	if (!cfgdir.exists(".hedgewars"))
       
    45 	{
       
    46 		if (!cfgdir.mkdir(".hedgewars"))
       
    47 		{
       
    48 			QMessageBox::critical(0,
       
    49 					tr("Error"),
       
    50 					tr("Cannot create directory %1").arg("/.hedgewars"),
       
    51 					tr("Quit"));
       
    52 			return ;
       
    53 		}
       
    54 	}
       
    55 	cfgdir.cd(".hedgewars");
       
    56 
       
    57 	QFile settings(cfgdir.absolutePath() + "/options");
       
    58 	if (settings.open(QIODevice::ReadOnly))
       
    59 	{
       
    60 		QTextStream stream(&settings);
       
    61 		stream.setCodec("UTF-8");
       
    62 		QString str;
       
    63 
       
    64 		while (!stream.atEnd())
       
    65 		{
       
    66 			str = stream.readLine();
       
    67 			if (str.startsWith(";")) continue;
       
    68 			if (str.startsWith("resolution "))
       
    69 			{
       
    70 				Form->ui.CBResolution->setCurrentIndex(str.mid(11).toLong());
       
    71 			} else
       
    72 			if (str.startsWith("fullscreen "))
       
    73 			{
       
    74 				Form->ui.CBFullscreen->setChecked(str.mid(11).toLong());
       
    75 			} else
       
    76 			if (str.startsWith("sound "))
       
    77 			{
       
    78 				Form->ui.CBEnableSound->setChecked(str.mid(6).toLong());
       
    79 			} else
       
    80 			if (str.startsWith("nick "))
       
    81 			{
       
    82 				Form->ui.editNetNick->setText(str.mid(5));
       
    83 			}
       
    84 		}
       
    85 		settings.close();
       
    86 	}
       
    87 }
       
    88 
       
    89 QStringList GameConfig::GetTeamsList()
       
    90 {
       
    91 	return cfgdir.entryList(QStringList("*.cfg"));
       
    92 }
       
    93 
       
    94 void GameConfig::SaveOptions()
       
    95 {
       
    96 	QFile settings(cfgdir.absolutePath() + "/options");
       
    97 	if (!settings.open(QIODevice::WriteOnly))
       
    98 	{
       
    99 		QMessageBox::critical(0,
       
   100 				tr("Error"),
       
   101 				tr("Cannot save options to file %1").arg(settings.fileName()),
       
   102 				tr("Quit"));
       
   103 		return ;
       
   104 	}
       
   105 	QTextStream stream(&settings);
       
   106 	stream.setCodec("UTF-8");
       
   107 	stream << "; Generated by Hedgewars, do not modify" << endl;
       
   108 	stream << "resolution " << Form->ui.CBResolution->currentIndex() << endl;
       
   109 	stream << "fullscreen " << Form->ui.CBFullscreen->isChecked() << endl;
       
   110 	stream << "sound " << Form->ui.CBEnableSound->isChecked() << endl;
       
   111 	stream << "nick " << Form->ui.editNetNick->text() << endl;
       
   112 	settings.close();
       
   113 }
       
   114 
       
   115 int GameConfig::vid_Resolution()
       
   116 {
       
   117 	return Form->ui.CBResolution->currentIndex();
       
   118 }
       
   119 
       
   120 bool GameConfig::vid_Fullscreen()
       
   121 {
       
   122 	return Form->ui.CBFullscreen->isChecked();
       
   123 }
       
   124 
       
   125 bool GameConfig::isSoundEnabled()
       
   126 {
       
   127 	return Form->ui.CBEnableSound->isChecked();
       
   128 }