]> git.sesse.net Git - vlc/blob - modules/gui/kde/preferences.cpp
kde.cpp: subscribe and unsubscribe messages
[vlc] / 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.5 2002/10/03 10:15:01 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  construct 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 **pp_parser;
55     vlc_list_t *p_list;
56     module_config_t *p_item;
57     QVBox *category_table = NULL;
58     QString *category_label;
59
60     this->p_intf = p_intf;
61
62     /* List all modules */
63     p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
64
65     /* Look for the selected module */
66     for( pp_parser = (module_t **)p_list->pp_objects ;
67          *pp_parser ;
68          pp_parser++ )
69     {
70
71         if( psz_module_name
72              && !strcmp( psz_module_name, (*pp_parser)->psz_object_name ) )
73         {
74             break;
75         }
76     }
77
78     if( !(*pp_parser) )
79     {
80         vlc_list_release( p_list );
81         return;
82     }
83
84     p_item = (*pp_parser)->p_config;
85     if( p_item ) do
86     {
87         switch( p_item->i_type )
88         {
89
90         case CONFIG_HINT_CATEGORY:
91         case CONFIG_HINT_END:
92
93             /*
94              * Now we can start taking care of the new category
95              */
96             if( p_item->i_type == CONFIG_HINT_CATEGORY )
97             {
98                 category_label = new QString( p_item->psz_text );
99                 QFrame *page = addPage( *category_label );
100                 QVBoxLayout *toplayout = new QVBoxLayout( page);
101                 QScrollView *sv = new QScrollView(page);
102                 sv->setResizePolicy(QScrollView::AutoOneFit);
103                 sv->setFrameStyle(QScrollView::NoFrame);
104                 toplayout->addWidget(sv);
105                 category_table = new QVBox(sv->viewport());
106                 sv->addChild(category_table);
107                 toplayout->addStretch(10);
108                 category_table->setSpacing(spacingHint());
109             }
110
111             break;
112
113         case CONFIG_ITEM_MODULE:
114
115             {
116                 
117                 vlc_mutex_lock( p_item->p_lock );
118                 KPluginsBox *item_frame =
119                     new KPluginsBox( p_intf, p_item->psz_text,
120                                      p_item->psz_value ? p_item->psz_value :"",
121                                      category_table,
122                                      spacingHint(),
123                                      this );
124                 QConfigItem *ci = new QConfigItem(this,
125                                                   p_item->psz_name,
126                                                   p_item->i_type,
127                                                   p_item->psz_value);
128                 connect(item_frame, SIGNAL(selectionChanged(const QString &)),
129                         ci, SLOT(setValue(const QString &)));
130
131                 
132                 /* build a list of available plugins */
133                 for( pp_parser = (module_t **)p_list->pp_objects ;
134                      *pp_parser ;
135                      pp_parser++ )
136                 {
137                     if( !strcmp( (*pp_parser)->psz_capability,
138                                  p_item->psz_type ) )
139                     {
140                         new QListViewItem(item_frame->getListView(),
141                                           (*pp_parser)->psz_object_name,
142                                           (*pp_parser)->psz_longname);
143                     }
144                 }
145
146                 vlc_mutex_unlock( p_item->p_lock );
147             }
148             break;
149
150         case CONFIG_ITEM_STRING:
151         case CONFIG_ITEM_FILE:
152
153             {
154                 QHBox *hb = new QHBox(category_table);
155                 hb->setSpacing(spacingHint());
156                 new QLabel(p_item->psz_text, hb);
157                 /* add input box with default value */
158                 vlc_mutex_lock( p_item->p_lock );
159                 
160                 KLineEdit *kl = new KLineEdit( p_item->psz_value ?
161                                                p_item->psz_value : "", hb);
162                 QConfigItem *ci = new QConfigItem(this, p_item->psz_name,
163                                                   p_item->i_type,
164                                                   p_item->psz_value ?
165                                                   p_item->psz_value : "");
166                 connect(kl, SIGNAL(textChanged ( const QString & )),
167                         ci, SLOT(setValue( const QString &)));
168                 QToolTip::add(kl, p_item->psz_longtext);
169                 kl->setMaxLength(10);
170                 
171                 vlc_mutex_unlock( p_item->p_lock );
172                 
173             }
174             break;
175
176         case CONFIG_ITEM_INTEGER:
177             /* add input box with default value */
178             {
179                 QHBox *hb = new QHBox(category_table);
180                 hb->setSpacing(spacingHint());
181                 new QLabel(p_item->psz_text, hb);                
182                 QSpinBox *item_adj = new QSpinBox(-1, 99999, 1, hb);
183                 QConfigItem *ci = new QConfigItem(this, p_item->psz_name,
184                                                   p_item->i_type,
185                                                   p_item->i_value);
186                 connect(item_adj, SIGNAL(valueChanged( int)),
187                         ci, SLOT(setValue(int)));
188                 QToolTip::add(item_adj, p_item->psz_longtext);
189                 item_adj->setValue( p_item->i_value );
190             }
191             break;
192
193         case CONFIG_ITEM_FLOAT:
194             {
195                 QHBox *hb = new QHBox(category_table);
196                 hb->setSpacing(spacingHint());
197                 new QLabel(p_item->psz_text, hb);                
198                 KDoubleNumInput *kdi= new KDoubleNumInput(p_item->f_value, hb);
199                 kdi->setRange(-1, 99999, 0.01, false);
200                 QConfigItem *ci = new QConfigItem(this, p_item->psz_name,
201                                                   p_item->i_type,
202                                                   p_item->f_value);
203                 connect(kdi, SIGNAL(valueChanged(double)),
204                         ci, SLOT(setValue(double)));
205                 QToolTip::add(kdi, p_item->psz_longtext);
206                 
207             }
208             break;
209                                                   
210                 
211         case CONFIG_ITEM_BOOL:
212
213             /* add check button */
214             {
215                 QCheckBox *bool_checkbutton =
216                     new QCheckBox(QString(p_item->psz_text), category_table);
217                 QConfigItem *ci = new QConfigItem(this, p_item->psz_name,
218                                                   p_item->i_type,
219                                                   p_item->i_value);
220                 bool_checkbutton->setChecked(p_item->i_value);
221                 connect(bool_checkbutton, SIGNAL(stateChanged( int)),
222                         ci, SLOT(setValue(int)));
223                 QToolTip::add(bool_checkbutton, p_item->psz_longtext);
224
225             }
226             break;
227
228         }
229
230         p_item++;
231     }
232     while( p_item->i_type != CONFIG_HINT_END );
233
234     vlc_list_release( p_list );
235
236     exec();
237 }
238
239 /*
240   empty destructor, qt takes care of this (I think)
241 */
242 KPreferences::~KPreferences()
243 {
244 }
245
246 /*
247   return true if the give module is configureable
248 */
249 bool KPreferences::isConfigureable(QString module)
250 {
251     module_t **pp_parser;
252     vlc_list_t *p_list;
253
254     p_list = vlc_list_find( this->p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
255
256     for( pp_parser = (module_t **)p_list->pp_objects ;
257          *pp_parser ;
258          pp_parser++ )
259     {
260         if( !module.compare( (*pp_parser)->psz_object_name ) )
261         {
262             bool ret = (*pp_parser)->i_config_items != 0;
263             vlc_list_release( p_list );
264             return ret;
265         }
266     }
267
268     vlc_list_release( p_list );
269     return false;
270 }
271
272 /*
273   run when the Apply button is pressed, and by the methods for the ok
274   and save buttons
275 */
276 void KPreferences::slotApply()
277 {
278     QObjectList * l = queryList( "QConfigItem" );
279     QObjectListIt it( *l );             // iterate over the config items
280     QObject * obj;
281     while ( (obj=it.current()) != 0 ) {
282         ++it;
283         QConfigItem *p_config = (QConfigItem *)obj;
284         msg_Dbg( p_intf, const_cast<char *>(p_config->name()));
285         msg_Dbg( p_intf, "%d", p_config->getType());
286
287         switch( p_config->getType() ) {
288
289         case CONFIG_ITEM_STRING:
290         case CONFIG_ITEM_FILE:
291         case CONFIG_ITEM_MODULE:
292             if (p_config->sValue()) {
293                 config_PutPsz( p_intf, p_config->name(),
294                                strdup(p_config->sValue().latin1()));
295             }
296             else {
297                 config_PutPsz( p_intf, p_config->name(), NULL );
298             }
299             break;
300         case CONFIG_ITEM_INTEGER:
301         case CONFIG_ITEM_BOOL:
302             config_PutInt( p_intf, p_config->name(), p_config->iValue() );
303             break;
304         case CONFIG_ITEM_FLOAT:
305             config_PutFloat( p_intf, p_config->name(), p_config->fValue() );
306             break;
307         }
308     }
309     delete l;
310 }
311
312 /*
313  run when the Ok button is pressed
314 */
315 void KPreferences::slotOk()
316 {
317     slotApply();
318     accept();
319 }
320
321 /*
322   run when the save button is pressed
323 */
324 void KPreferences::slotUser1()
325 {
326     slotApply();
327     config_SaveConfigFile( p_intf, NULL );
328 }