File indexing completed on 2024-05-12 05:46:31

0001 /*****************************************************************************
0002  * This file is part of Kiten, a KDE Japanese Reference Tool...              *
0003  * Copyright (C) 2005 Paul Temple <paul.temple@gmx.net>                      *
0004  * Copyright (C) 2006 Joseph Kerian <jkerian@gmail.com>                      *
0005  * Copyright (C) 2006 Eric Kjeldergaard <kjelderg@gmail.com>                 *
0006  *                                                                           *
0007  * This program is free software; you can redistribute it and/or modify      *
0008  * it under the terms of the GNU General Public License as published by      *
0009  * the Free Software Foundation; either version 2 of the License, or         *
0010  * (at your option) any later version.                                       *
0011  *                                                                           *
0012  * This program is distributed in the hope that it will be useful,           *
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
0015  * GNU General Public License for more details.                              *
0016  *                                                                           *
0017  * You should have received a copy of the GNU General Public License         *
0018  * along with this program; if not, write to the Free Software               *
0019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 *
0020  * USA                                                                       *
0021  *****************************************************************************/
0022 
0023 #include "configdictionaryselector.h"
0024 
0025 #include <KConfigSkeleton>
0026 
0027 #include <QFileDialog>
0028 #include <QStringList>
0029 
0030 ConfigDictionarySelector::ConfigDictionarySelector( const QString &dictionaryName,
0031   QWidget *parent, KConfigSkeleton *config,Qt::WindowFlags f )
0032 {
0033   setupUi( this );
0034   _dictName = dictionaryName;
0035   _config = config;
0036 
0037   connect(addButton, &QPushButton::clicked, this, &ConfigDictionarySelector::addDictSlot);
0038   connect(delButton, &QPushButton::clicked, this, &ConfigDictionarySelector::deleteDictSlot);
0039   __useGlobal->setObjectName( QString( "kcfg_" + _dictName + "__useGlobal" ) );
0040 }
0041 
0042 //Read from preferences to the active list
0043 void ConfigDictionarySelector::updateWidgets()
0044 {
0045   QStringList names;
0046 
0047   _config->setCurrentGroup( "dicts_" + _dictName );
0048   KConfigSkeletonItem *item = _config->findItem( _dictName + "__NAMES" );
0049   if( item != nullptr )
0050   {
0051     names = item->property().toStringList();
0052   }
0053 
0054   foreach( const QString &it, names )
0055   {
0056     QString name = _dictName + '_' + it;
0057     if ( ! _config->findItem( name ) )
0058     {
0059       _config->addItem( new KConfigSkeleton::ItemString( _dictName, it, *new QString() ), name );
0060       //Don't touch the *new QString()... that's a reference for a reason... stupid KDE
0061     }
0062   }
0063 
0064   _config->load();
0065   fileList->clear();
0066 
0067   foreach( const QString &it, names )
0068   {
0069     QStringList newRow( it );
0070     newRow << _config->findItem( _dictName + '_' + it )->property().toString();
0071     (void) new QTreeWidgetItem( fileList, newRow );
0072   }
0073 }
0074 
0075 void ConfigDictionarySelector::updateSettings()
0076 {
0077   QStringList names;
0078 
0079   KConfigGroup group = _config->config()->group( "dicts_" + _dictName.toLower() );
0080 
0081   for( int i = 0; i < fileList->topLevelItemCount(); i++ )
0082   {
0083     QTreeWidgetItem *it = fileList->topLevelItem( i );
0084     QString dictionaryName = it->text( 0 );
0085     QString dictionaryPath = it->text( 1 );
0086     names.append( dictionaryName );
0087 
0088     if ( ! group.hasKey( dictionaryName ) )
0089     {
0090       KConfigSkeletonItem *item = new KConfigSkeleton::ItemPath( group.name()
0091                                                 , dictionaryName, *new QString() );
0092       _config->addItem( item, dictionaryName );
0093     }
0094     group.writeEntry( dictionaryName, dictionaryPath );
0095   }
0096 
0097   //This feels distinctly hackish to me... :(
0098   _config->findItem( _dictName + "__NAMES" )->setProperty( names );
0099   _config->save();
0100 }
0101 
0102 void ConfigDictionarySelector::updateWidgetsDefault()
0103 {
0104   // no default for custom edict list or
0105   // should we really delete all items in the list?
0106 }
0107 
0108 bool ConfigDictionarySelector::isDefault()
0109 {
0110   // no default for custom edict list or
0111   // should we really delete all items in the list?
0112   return true;
0113 }
0114 
0115 bool ConfigDictionarySelector::hasChanged()
0116 {
0117   return false;
0118 }
0119 
0120 void ConfigDictionarySelector::addDictSlot()
0121 {
0122   QTreeWidgetItem *item = fileList->topLevelItem( 0 );
0123 
0124   QString filename = QFileDialog::getOpenFileName(nullptr, QString(),
0125                   item ? QFileInfo( item->text( 1 ) ).absolutePath().append( "/" )
0126                   : QString() );
0127   QString name = QFileInfo( filename ).fileName();
0128   if( filename.isNull() )
0129     return;
0130 
0131   QStringList newRow( name );
0132   newRow << filename;
0133   (void) new QTreeWidgetItem( fileList, newRow );
0134 
0135   updateSettings();
0136   emit widgetChanged();
0137 }
0138 
0139 void ConfigDictionarySelector::deleteDictSlot()
0140 {
0141   foreach( QTreeWidgetItem *file, fileList->selectedItems() )
0142   {
0143     if ( ! file )
0144     {
0145       return;
0146     }
0147 
0148     delete file;
0149 
0150     updateSettings();
0151     emit widgetChanged();
0152   }
0153 }
0154 
0155