ALL: added GPL headers to my files. Some of the files from the original
[vlc.git] / modules / gui / kde / preferences.cpp
1 /*****************************************************************************
2  * preferences.cpp: preferences window for the kde gui
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: preferences.cpp,v 1.2 2002/08/12 17:38:10 sigmunau Exp $
6  *
7  * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no> Mon Aug 12 2002
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23 #include <kdialogbase.h>
24 #include <qmap.h>
25 #include <qcheckbox.h>
26 #include <qframe.h>
27 #include <qgroupbox.h>
28 #include <qlayout.h>
29 #include <qlabel.h>
30 #include <qlistview.h>
31 #include <qnamespace.h>
32 #include <qobjectlist.h>
33 #include <qspinbox.h>
34 #include <qtooltip.h>
35 #include <qvbox.h>
36
37 #include <kbuttonbox.h>
38 #include <klineedit.h>
39 #include <klocale.h>
40 #include <knuminput.h>
41
42 #include "QConfigItem.h"
43 #include "pluginsbox.h"
44 #include "preferences.h"
45
46 /*
47  construkt a new configuration window for the given module
48 */
49 KPreferences::KPreferences(intf_thread_t *p_intf, const char *psz_module_name,
50                            QWidget *parent, const QString &caption) :
51     KDialogBase ( Tabbed, caption, Ok| Apply|Cancel|User1, Ok, parent,
52                   "vlc preferences", true, false, "Save")
53 {
54     module_t *p_module, *p_module_bis;
55     module_config_t *p_item;
56     QVBox *category_table = NULL;
57     QString *category_label;
58
59     this->p_intf = p_intf;
60
61     /* Look for the selected module */
62     for( p_module = p_intf->p_vlc->p_module_bank->first ; p_module != NULL ;
63          p_module = p_module->next )
64     {
65
66         if( psz_module_name && !strcmp( psz_module_name, p_module->psz_object_name ) )
67             break;
68     }
69     if( !p_module ) return;
70     p_item = p_module->p_config;
71     do
72     {
73         switch( p_item->i_type )
74         {
75
76         case CONFIG_HINT_CATEGORY:
77         case CONFIG_HINT_END:
78
79             /*
80              * Now we can start taking care of the new category
81              */
82             if( p_item->i_type == CONFIG_HINT_CATEGORY )
83             {
84                 category_label = new QString( p_item->psz_text );
85                 QFrame *page = addPage( *category_label );
86                 QVBoxLayout *toplayout = new QVBoxLayout( page);
87                 QScrollView *sv = new QScrollView(page);
88                 sv->setResizePolicy(QScrollView::AutoOneFit);
89                 sv->setFrameStyle(QScrollView::NoFrame);
90                 toplayout->addWidget(sv);
91                 category_table = new QVBox(sv->viewport());
92                 sv->addChild(category_table);
93                 toplayout->addStretch(10);
94                 category_table->setSpacing(spacingHint());
95             }
96
97             break;
98
99         case CONFIG_ITEM_MODULE:
100
101             {
102                 
103                 vlc_mutex_lock( p_item->p_lock );
104                 KPluginsBox *item_frame =
105                     new KPluginsBox( p_intf, p_item->psz_text,
106                                      p_item->psz_value ? p_item->psz_value :"",
107                                      category_table,
108                                      spacingHint(),
109                                      this );
110                 QConfigItem *ci = new QConfigItem(this,
111                                                   p_item->psz_name,
112                                                   p_item->i_type,
113                                                   p_item->psz_value);
114                 connect(item_frame, SIGNAL(selectionChanged(const QString &)),
115                         ci, SLOT(setValue(const QString &)));
116
117                 
118                 /* build a list of available plugins */
119                 
120 #if 0 /* FIXME */
121                 for( p_module_bis = p_intf->p_vlc->p_module_bank->first ;
122                      p_module_bis != NULL ;
123                      p_module_bis = p_module_bis->next ) {
124                     if( p_module_bis->i_capabilities & (1 << p_item->i_value)){
125                         new QListViewItem(item_frame->getListView(),
126                                           p_module_bis->psz_object_name,
127                                           p_module_bis->psz_longname);
128                     }
129                 }
130 #endif
131                 vlc_mutex_unlock( p_item->p_lock );
132             }
133             break;
134
135         case CONFIG_ITEM_STRING:
136         case CONFIG_ITEM_FILE:
137
138             {
139                 QHBox *hb = new QHBox(category_table);
140                 hb->setSpacing(spacingHint());
141                 new QLabel(p_item->psz_text, hb);
142                 /* add input box with default value */
143                 vlc_mutex_lock( p_item->p_lock );
144                 
145                 KLineEdit *kl = new KLineEdit( p_item->psz_value ?
146                                                p_item->psz_value : "", hb);
147                 QConfigItem *ci = new QConfigItem(this, p_item->psz_name,
148                                                   p_item->i_type,
149                                                   p_item->psz_value ?
150                                                   p_item->psz_value : "");
151                 connect(kl, SIGNAL(textChanged ( const QString & )),
152                         ci, SLOT(setValue( const QString &)));
153                 QToolTip::add(kl, p_item->psz_longtext);
154                 kl->setMaxLength(10);
155                 
156                 vlc_mutex_unlock( p_item->p_lock );
157                 
158             }
159             break;
160
161         case CONFIG_ITEM_INTEGER:
162             /* add input box with default value */
163             {
164                 QHBox *hb = new QHBox(category_table);
165                 hb->setSpacing(spacingHint());
166                 new QLabel(p_item->psz_text, hb);                
167                 QSpinBox *item_adj = new QSpinBox(-1, 99999, 1, hb);
168                 QConfigItem *ci = new QConfigItem(this, p_item->psz_name,
169                                                   p_item->i_type,
170                                                   p_item->i_value);
171                 connect(item_adj, SIGNAL(valueChanged( int)),
172                         ci, SLOT(setValue(int)));
173                 QToolTip::add(item_adj, p_item->psz_longtext);
174                 item_adj->setValue( p_item->i_value );
175             }
176             break;
177
178         case CONFIG_ITEM_FLOAT:
179             {
180                 QHBox *hb = new QHBox(category_table);
181                 hb->setSpacing(spacingHint());
182                 new QLabel(p_item->psz_text, hb);                
183                 KDoubleNumInput *kdi= new KDoubleNumInput(p_item->f_value, hb);
184                 kdi->setRange(-1, 99999, 0.01, false);
185                 QConfigItem *ci = new QConfigItem(this, p_item->psz_name,
186                                                   p_item->i_type,
187                                                   p_item->f_value);
188                 connect(kdi, SIGNAL(valueChanged(double)),
189                         ci, SLOT(setValue(double)));
190                 QToolTip::add(kdi, p_item->psz_longtext);
191                 
192             }
193             break;
194                                                   
195                 
196         case CONFIG_ITEM_BOOL:
197
198             /* add check button */
199             {
200                 QCheckBox *bool_checkbutton =
201                     new QCheckBox(QString(p_item->psz_text), category_table);
202                 QConfigItem *ci = new QConfigItem(this, p_item->psz_name,
203                                                   p_item->i_type,
204                                                   p_item->i_value);
205                 bool_checkbutton->setChecked(p_item->i_value);
206                 connect(bool_checkbutton, SIGNAL(stateChanged( int)),
207                         ci, SLOT(setValue(int)));
208                 QToolTip::add(bool_checkbutton, p_item->psz_longtext);
209
210             }
211             break;
212
213         }
214
215         p_item++;
216     }
217     while( p_item->i_type != CONFIG_HINT_END );
218     exec();
219     
220 }
221
222 /*
223   empty destructor, qt takes care of this (I think)
224 */
225 KPreferences::~KPreferences()
226 {
227 }
228
229 /*
230   return true if the give module is configureable
231 */
232 bool KPreferences::isConfigureable(QString module)
233 {
234     module_t *p_module;
235     for( p_module = p_intf->p_vlc->p_module_bank->first ;
236          p_module != NULL ;
237          p_module = p_module->next ) {
238         if( !module.compare( p_module->psz_object_name ) ) {
239             return p_module->i_config_items != 0;
240         }
241     }
242     return false;
243
244 }
245
246 /*
247   run when the Apply button is pressed, and by the methods for the ok
248   and save buttons
249 */
250 void KPreferences::slotApply()
251 {
252     QObjectList * l = queryList( "QConfigItem" );
253     QObjectListIt it( *l );             // iterate over the config items
254     QObject * obj;
255     while ( (obj=it.current()) != 0 ) {
256         ++it;
257         QConfigItem *p_config = (QConfigItem *)obj;
258         msg_Dbg( p_intf, const_cast<char *>(p_config->name()));
259         msg_Dbg( p_intf, "%d", p_config->getType());
260
261         switch( p_config->getType() ) {
262
263         case CONFIG_ITEM_STRING:
264         case CONFIG_ITEM_FILE:
265         case CONFIG_ITEM_MODULE:
266             if (p_config->sValue()) {
267                 config_PutPsz( p_intf, p_config->name(),
268                                strdup(p_config->sValue().latin1()));
269             }
270             else {
271                 config_PutPsz( p_intf, p_config->name(), NULL );
272             }
273             break;
274         case CONFIG_ITEM_INTEGER:
275         case CONFIG_ITEM_BOOL:
276             config_PutInt( p_intf, p_config->name(), p_config->iValue() );
277             break;
278         case CONFIG_ITEM_FLOAT:
279             config_PutFloat( p_intf, p_config->name(), p_config->fValue() );
280             break;
281         }
282     }
283     delete l;
284 }
285
286 /*
287  run when the Ok button is pressed
288 */
289 void KPreferences::slotOk()
290 {
291     slotApply();
292     accept();
293 }
294
295 /*
296   run when the save button is pressed
297 */
298 void KPreferences::slotUser1()
299 {
300     slotApply();
301     config_SaveConfigFile( p_intf, NULL );
302 }