File indexing completed on 2024-05-05 04:51:38
0001 /* 0002 SPDX-FileCopyrightText: 2003-2008 Sebastian Trueg <trueg@k3b.org> 0003 SPDX-FileCopyrightText: 2010 Michal Malek <michalm@jabster.pl> 0004 SPDX-FileCopyrightText: 1998-2010 Sebastian Trueg <trueg@k3b.org> 0005 0006 SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 0009 0010 #include "k3bthemeoptiontab.h" 0011 #include "k3bapplication.h" 0012 #include "k3bthememanager.h" 0013 #include "k3bthememodel.h" 0014 0015 #include <KTar> 0016 #include <KConfig> 0017 #include <KLocalizedString> 0018 #include <KIO/Global> 0019 #include <KIO/StoredTransferJob> 0020 #include <KUrlRequester> 0021 #include <KUrlRequesterDialog> 0022 #include <KMessageBox> 0023 0024 #include <QFile> 0025 #include <QFileInfo> 0026 #include <QPointer> 0027 #include <QStandardPaths> 0028 #include <QTemporaryFile> 0029 #include <QItemSelectionModel> 0030 #include <QLabel> 0031 0032 0033 K3b::ThemeOptionTab::ThemeOptionTab( QWidget* parent ) 0034 : QWidget( parent ), 0035 m_themeModel( new ThemeModel( k3bappcore->themeManager(), this ) ) 0036 { 0037 setupUi( this ); 0038 0039 m_centerPreviewLabel->setAutoFillBackground( true ); 0040 m_leftPreviewLabel->setAutoFillBackground( true ); 0041 m_rightPreviewLabel->setAutoFillBackground( true ); 0042 0043 m_viewTheme->setModel( m_themeModel ); 0044 0045 connect( m_viewTheme->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), 0046 this, SLOT(selectionChanged()) ); 0047 connect( m_buttonInstallTheme, SIGNAL(clicked()), 0048 this, SLOT(slotInstallTheme()) ); 0049 connect( m_buttonRemoveTheme, SIGNAL(clicked()), 0050 this, SLOT(slotRemoveTheme()) ); 0051 connect(m_buttonGetNewThemes, &KNSWidgets::Button::dialogFinished, this, [this] (const QList<KNSCore::Entry> &changedEntries) { 0052 if (!changedEntries.isEmpty()) { 0053 m_themeModel->reload(); 0054 } 0055 }); 0056 m_buttonGetNewThemes->setConfigFile("k3btheme.knsrc"); 0057 } 0058 0059 0060 K3b::ThemeOptionTab::~ThemeOptionTab() 0061 { 0062 } 0063 0064 0065 void K3b::ThemeOptionTab::readSettings() 0066 { 0067 m_themeModel->reload(); 0068 0069 QModelIndex index = m_themeModel->indexForTheme( k3bappcore->themeManager()->currentTheme() ); 0070 m_viewTheme->setCurrentIndex( index ); 0071 } 0072 0073 0074 bool K3b::ThemeOptionTab::saveSettings() 0075 { 0076 QModelIndex index = m_viewTheme->currentIndex(); 0077 if( Theme* theme = m_themeModel->themeForIndex( index ) ) { 0078 k3bappcore->themeManager()->setCurrentTheme( theme ); 0079 } 0080 return true; 0081 } 0082 0083 0084 bool K3b::ThemeOptionTab::event( QEvent *event ) 0085 { 0086 if( event->type() == QEvent::StyleChange ) { 0087 selectionChanged(); 0088 } 0089 return QWidget::event( event ); 0090 } 0091 0092 0093 void K3b::ThemeOptionTab::selectionChanged() 0094 { 0095 QModelIndex index = m_viewTheme->currentIndex(); 0096 if( Theme* theme = m_themeModel->themeForIndex( index ) ) { 0097 m_centerPreviewLabel->setText( i18n("K3b - The CD/DVD Kreator") ); 0098 0099 QPalette pal( palette() ); 0100 pal.setColor( backgroundRole(), theme->backgroundColor() ); 0101 pal.setColor( foregroundRole(), theme->backgroundColor() ); 0102 m_centerPreviewLabel->setPalette( pal ); 0103 m_leftPreviewLabel->setPalette( pal ); 0104 m_rightPreviewLabel->setPalette( pal ); 0105 0106 m_leftPreviewLabel->setPixmap( theme->pixmap( K3b::Theme::PROJECT_LEFT ) ); 0107 m_rightPreviewLabel->setPixmap( theme->pixmap( K3b::Theme::PROJECT_RIGHT ) ); 0108 0109 m_buttonRemoveTheme->setEnabled( theme->local() ); 0110 } 0111 } 0112 0113 0114 void K3b::ThemeOptionTab::slotInstallTheme() 0115 { 0116 QUrl themeURL = KUrlRequesterDialog::getUrl( QUrl(), this, 0117 i18n("Drag or Type Theme URL") ); 0118 0119 if( themeURL.url().isEmpty() ) 0120 return; 0121 0122 QTemporaryFile themeTmpFile; 0123 KIO::StoredTransferJob* transferJob = KIO::storedGet( themeURL ); 0124 bool transferJobSucceed = true; 0125 connect( transferJob, &KJob::result, [&](KJob*) { 0126 if( transferJob->error() != KJob::NoError ) { 0127 themeTmpFile.open(); 0128 themeTmpFile.write( transferJob->data() ); 0129 themeTmpFile.close(); 0130 } else { 0131 transferJobSucceed = false; 0132 } 0133 } ); 0134 0135 if( transferJob->exec() && !transferJobSucceed ) { 0136 QString sorryText; 0137 QString tmpArg = themeURL.toDisplayString(); 0138 if (themeURL.isLocalFile()) 0139 sorryText = i18n("Unable to find the icon theme archive %1.",tmpArg); 0140 else 0141 sorryText = i18n("Unable to download the icon theme archive.\n" 0142 "Please check that address %1 is correct.",tmpArg); 0143 KMessageBox::error( this, sorryText ); 0144 return; 0145 } 0146 0147 // check if the archive contains a dir with a k3b.theme file 0148 QString themeName; 0149 KTar archive( &themeTmpFile ); 0150 archive.open(QIODevice::ReadOnly); 0151 const KArchiveDirectory* themeDir = archive.directory(); 0152 QStringList entries = themeDir->entries(); 0153 bool validThemeArchive = false; 0154 if( entries.count() > 0 ) { 0155 if( themeDir->entry(entries.first())->isDirectory() ) { 0156 const KArchiveDirectory* subDir = dynamic_cast<const KArchiveDirectory*>( themeDir->entry(entries.first()) ); 0157 themeName = subDir->name(); 0158 if( subDir && subDir->entry( "k3b.theme" ) ) { 0159 validThemeArchive = true; 0160 0161 // check for all necessary pixmaps (this is a little evil hacking) 0162 for( int i = 0; i <= K3b::Theme::WELCOME_BG; ++i ) { 0163 if( !subDir->entry( K3b::Theme::filenameForPixmapType( (K3b::Theme::PixmapType)i ) ) ) { 0164 validThemeArchive = false; 0165 break; 0166 } 0167 } 0168 } 0169 } 0170 } 0171 0172 if( !validThemeArchive ) { 0173 KMessageBox::error( this, i18n("The file is not a valid K3b theme archive.") ); 0174 } 0175 else { 0176 QString themeBasePath = QStandardPaths::writableLocation( QStandardPaths::GenericDataLocation ) + "/k3b/pics/"; 0177 QDir().mkpath( themeBasePath ); 0178 0179 // check if there already is a theme by that name 0180 if( !QFile::exists( themeBasePath + '/' + themeName ) || 0181 KMessageBox::warningTwoActions( this, 0182 i18n("A theme with the name '%1' already exists. Do you want to " 0183 "overwrite it?", themeName), 0184 i18n("Theme exists"), 0185 KStandardGuiItem::overwrite(), 0186 KStandardGuiItem::cancel() ) == KMessageBox::PrimaryAction ) { 0187 // install the theme 0188 archive.directory()->copyTo( themeBasePath ); 0189 } 0190 } 0191 0192 archive.close(); 0193 0194 readSettings(); 0195 } 0196 0197 0198 void K3b::ThemeOptionTab::slotRemoveTheme() 0199 { 0200 QModelIndex index = m_viewTheme->currentIndex(); 0201 if( Theme* theme = m_themeModel->themeForIndex( index ) ) { 0202 QString question=i18n("<qt>Are you sure you want to remove the " 0203 "<strong>%1</strong> theme?<br>" 0204 "<br>" 0205 "This will delete the files installed by this theme.</qt>", theme->name() ); 0206 0207 if( KMessageBox::warningContinueCancel( this, question, i18n("Delete"), KStandardGuiItem::del() ) != KMessageBox::Continue ) 0208 return; 0209 0210 m_themeModel->removeRow( index.row() ); 0211 0212 // reread the themes (this will also set the default theme in case we delete the 0213 // selected one) 0214 readSettings(); 0215 } 0216 } 0217 0218 #include "moc_k3bthemeoptiontab.cpp"