fix translating messages from .xml file, now the the messages don't contain entities
[opensuse:kdebase4-opensuse.git] / greeter / greetings.cpp
1 /*
2  *
3  * Copyright (C) 2001 Adrian Schroeter <adrian@suse.de>
4  *
5  */
6
7 #include <qwidget.h>
8 #include <qlayout.h>
9 #include <qcheckbox.h>
10 #include <qstring.h>
11 #include <qspinbox.h>
12 #include <qregexp.h>
13 #include <qlabel.h>
14 #include <q3whatsthis.h>
15 #include <qlayout.h>
16 #include <qtimer.h>
17 #include <qfile.h>
18 #include <qtextstream.h>
19 #include <qdom.h>
20 #include <QShowEvent>
21 #include <qscrollbar.h>
22
23 #include <kapplication.h>
24 #include <kconfig.h>
25 #include <kglobal.h>
26 #include <kstandarddirs.h>
27 #include <klocale.h>
28 #include <kcmodule.h>
29 #include <kprocess.h>
30 #include <kapplication.h>
31
32 #include "greetings.h"
33
34 /**** SUSEgreetings ****/
35
36 SUSEgreetings::SUSEgreetings()
37     : QDialog(0, Qt::FramelessWindowHint | Qt::CustomizeWindowHint )
38 {
39     QFile f;
40     QString content;
41
42     // header 
43     QString filename = KStandardDirs::locate( "data", "SUSEgreeter/greet_header.html" );
44     f.setFileName(filename);
45     if ( ! f.open(QIODevice::ReadOnly))
46     {
47         // %1 = filename
48         content = i18n("Error loading %1", QLatin1String("greet_header.html"));
49     }
50     else
51     {
52         QTextStream stream(&f);
53         content = stream.readAll();
54         stream.reset();
55         f.close();
56     }
57
58     // section template
59     QString section_template;
60     filename = KStandardDirs::locate( "data", "SUSEgreeter/greet_section.html" );
61     f.setFileName(filename);
62     if ( ! f.open(QIODevice::ReadOnly))
63     {
64         // %1 = filename
65          section_template = i18n("Error loading %1", QLatin1String("greet_section.html"));
66     }
67     else
68     {
69         QTextStream stream(&f);
70         section_template = stream.readAll();
71         stream.reset();
72         f.close();
73     }
74
75     // sections
76     filename = KStandardDirs::locate( "data", "SUSEgreeter/greet.xml" );
77     f.setFileName(filename);
78     if ( ! f.open(QIODevice::ReadOnly))
79     {
80         // %1 = filename
81         content += i18n("Error loading %1", QLatin1String("greet.xml"));
82     }
83     else
84     {
85         QDomDocument doc( "sections" );
86         if ( !doc.setContent( &f )) {
87            content += i18n("Error loading %1", QLatin1String("greet.xml"));
88         }
89         else {
90            QDomElement docElem = doc.documentElement();
91            QDomNode n = docElem.firstChild();
92            while( !n.isNull() ) {
93                QDomElement e = n.toElement();
94                if( !e.isNull() ) {
95                    if (e.tagName()=="section") {
96                       QString section = section_template;
97                       QDomNode ex = n.firstChild();
98                       while ( !ex.isNull() ) {
99                          QDomElement e2 = ex.toElement();
100                          if( !e2.isNull() ) {
101                             if (e2.tagName()=="icon") {
102                                 section.replace("__ICON__", e2.text() );
103                             }
104                             else if (e2.tagName()=="title") {
105                                 section.replace("__TITLE__", i18n(e2.text().toAscii()) );
106                             }
107                             else if (e2.tagName()=="text") {
108                                 section.replace("__TEXT__", i18n(e2.text().toAscii()));
109                             }
110                          }
111                          ex = ex.nextSibling();
112                       }
113                       content += section;
114                    }
115                }
116                n = n.nextSibling();
117            }
118         }
119         f.close();
120     }
121
122     // footer
123     filename = KStandardDirs::locate( "data", "SUSEgreeter/greet_footer.html" );
124     f.setFileName(filename);
125     if ( ! f.open(QIODevice::ReadOnly))
126     {
127         // %1 = filename
128         content = i18n("Error loading %1", QLatin1String("greet_footer.html"));
129     }
130     else
131     {
132         QTextStream stream(&f);
133         content += stream.readAll();
134         stream.reset();
135         f.close();
136     }
137     content.replace( "__ENJOY_OPENSUSE__", i18n("Have a lot of fun!"));
138     content.replace( "__CLOSE__", i18n("CLOSE"));
139     content.replace( "__INTRODUCTION__", i18n("Show Introduction to KDE4"));
140
141     Text = new KTextBrowser( this, "TextView" );
142     Text->setText( content );
143     Text->setNotifyClick( true );
144     Text->setFrameStyle( QFrame::NoFrame );
145     QHBoxLayout *top = new QHBoxLayout(this);
146     top->setContentsMargins( 0, 0, 0, 0 );
147     top->addWidget(Text);
148     setFixedSize( 800, 600 );
149     top->activate();
150
151     QFont font;
152     font.setPixelSize(17);
153     font.setFamily("sans serif");
154     Text->setFont( font );
155     Text->adjustSize();
156
157     top->activate();
158
159     connect( Text, SIGNAL(urlClick(const QString&)), this, SLOT(urlClick(const QString&)) );
160 }
161
162 void SUSEgreetings::showEvent(QShowEvent* e)
163 {
164     QDialog::showEvent(e);
165
166     adjustSize();
167 }
168
169 void SUSEgreetings::adjustSize()
170 {
171     if (Text->verticalScrollBar()->isVisible()) {
172         QFont font = Text->font();
173         if (font.pixelSize() < 14)
174             return;
175
176         font.setPixelSize(font.pixelSize()-1);
177         Text->setFont(font);
178
179         QTimer::singleShot(0, this, SLOT(adjustSize()));
180     }
181 }
182
183 SUSEgreetings::~SUSEgreetings()
184 {
185 }
186
187 void SUSEgreetings::urlClick(const QString &s){
188     // to get rid of const
189     QString url(s);
190
191     if (url.startsWith("/close")) {
192         kapp->quit();
193         return;
194     }
195
196     if (url=="http://software.opensuse.org/")
197       url="http://software.opensuse.org/search?baseproject=openSUSE%3A11.3";
198
199     if ( url.startsWith( "/you" ) ) {
200         KProcess::startDetached("kdesu", QStringList() << "/sbin/yast2" << "online_update");
201     } else if ( url.startsWith( "irc://") ) {
202         KProcess::startDetached("konversationircprotocolhandler", QStringList() << url);
203     } else if ( url.startsWith( "/introduction" ) ) {
204         url="http://help.opensuse.org/kde4/";
205         KProcess::startDetached("kfmclient", QStringList() << "openURL" << url);
206     } else {
207         KProcess::startDetached("kfmclient", QStringList() << "openURL" << url);
208     }
209
210     // give the user some feedback
211     QApplication::changeOverrideCursor(Qt::WaitCursor);
212     QTimer::singleShot(2000, this, SLOT(resetCursor()));
213 };
214
215 void SUSEgreetings::resetCursor(){
216     QApplication::restoreOverrideCursor();
217 };
218
219 int SUSEgreetings::buttons()
220 {
221     return 0 ;
222 }
223
224 QString SUSEgreetings::quickHelp() const
225 {
226     return QString("");
227 }
228
229 #include "greetings.moc"