kmetaprops.cpp
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2001,2002 Rolf Magnus <ramagnus@kde.org> 00003 00004 library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License version 2 as published by the Free Software Foundation. 00007 00008 This library is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 Library General Public License for more details. 00012 00013 You should have received a copy of the GNU Library General Public License 00014 along with this library; see the file COPYING.LIB. If not, write to 00015 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00016 Boston, MA 02110-1301, USA. 00017 00018 $Id: kmetaprops.cpp 532794 2006-04-22 20:33:21Z pfeiffer $ 00019 */ 00020 00021 #include "kmetaprops.h" 00022 00023 #include <kdebug.h> 00024 #include <kfilemetainfowidget.h> 00025 #include <kfilemetainfo.h> 00026 #include <kglobal.h> 00027 #include <kglobalsettings.h> 00028 #include <klocale.h> 00029 #include <kprotocolinfo.h> 00030 00031 #include <qvalidator.h> 00032 #include <qlayout.h> 00033 #include <qlabel.h> 00034 #include <qfileinfo.h> 00035 #include <qdatetime.h> 00036 #include <qstylesheet.h> 00037 #include <qvgroupbox.h> 00038 00039 #undef Bool 00040 00041 class MetaPropsScrollView : public QScrollView 00042 { 00043 public: 00044 MetaPropsScrollView(QWidget* parent = 0, const char* name = 0) 00045 : QScrollView(parent, name) 00046 { 00047 setFrameStyle(QFrame::NoFrame); 00048 m_frame = new QFrame(viewport(), "MetaPropsScrollView::m_frame"); 00049 m_frame->setFrameStyle(QFrame::NoFrame); 00050 addChild(m_frame, 0, 0); 00051 }; 00052 00053 QFrame* frame() {return m_frame;}; 00054 00055 protected: 00056 virtual void viewportResizeEvent(QResizeEvent* ev) 00057 { 00058 QScrollView::viewportResizeEvent(ev); 00059 m_frame->resize( kMax(m_frame->sizeHint().width(), ev->size().width()), 00060 kMax(m_frame->sizeHint().height(), ev->size().height())); 00061 }; 00062 00063 private: 00064 QFrame* m_frame; 00065 }; 00066 00067 class KFileMetaPropsPlugin::KFileMetaPropsPluginPrivate 00068 { 00069 public: 00070 KFileMetaPropsPluginPrivate() {} 00071 ~KFileMetaPropsPluginPrivate() {} 00072 00073 QFrame* m_frame; 00074 QGridLayout* m_framelayout; 00075 KFileMetaInfo m_info; 00076 // QPushButton* m_add; 00077 QPtrList<KFileMetaInfoWidget> m_editWidgets; 00078 }; 00079 00080 KFileMetaPropsPlugin::KFileMetaPropsPlugin(KPropertiesDialog* props) 00081 : KPropsDlgPlugin(props) 00082 { 00083 d = new KFileMetaPropsPluginPrivate; 00084 00085 KFileItem * fileitem = properties->item(); 00086 kdDebug(250) << "KFileMetaPropsPlugin constructor" << endl; 00087 00088 d->m_info = fileitem->metaInfo(); 00089 if (!d->m_info.isValid()) 00090 { 00091 d->m_info = KFileMetaInfo(properties->kurl().path(-1)); 00092 fileitem->setMetaInfo(d->m_info); 00093 } 00094 00095 if ( properties->items().count() > 1 ) 00096 { 00097 // not yet supported 00098 // we should allow setting values for a list of files. Itt makes sense 00099 // in some cases, like the album of a list of mp3s 00100 return; 00101 } 00102 00103 createLayout(); 00104 00105 setDirty(true); 00106 } 00107 00108 void KFileMetaPropsPlugin::createLayout() 00109 { 00110 QFileInfo file_info(properties->item()->url().path()); 00111 00112 kdDebug(250) << "KFileMetaPropsPlugin::createLayout" << endl; 00113 00114 // is there any valid and non-empty info at all? 00115 if ( !d->m_info.isValid() || (d->m_info.preferredKeys()).isEmpty() ) 00116 return; 00117 00118 // now get a list of groups 00119 KFileMetaInfoProvider* prov = KFileMetaInfoProvider::self(); 00120 QStringList groupList = d->m_info.preferredGroups(); 00121 00122 const KFileMimeTypeInfo* mtinfo = prov->mimeTypeInfo(d->m_info.mimeType()); 00123 if (!mtinfo) 00124 { 00125 kdDebug(7034) << "no mimetype info there\n"; 00126 return; 00127 } 00128 00129 // let the dialog create the page frame 00130 QFrame* topframe = properties->addPage(i18n("&Meta Info")); 00131 topframe->setFrameStyle(QFrame::NoFrame); 00132 QVBoxLayout* tmp = new QVBoxLayout(topframe); 00133 00134 // create a scroll view in the page 00135 MetaPropsScrollView* view = new MetaPropsScrollView(topframe); 00136 00137 tmp->addWidget(view); 00138 00139 d->m_frame = view->frame(); 00140 00141 QVBoxLayout *toplayout = new QVBoxLayout(d->m_frame); 00142 toplayout->setSpacing(KDialog::spacingHint()); 00143 00144 for (QStringList::Iterator git=groupList.begin(); 00145 git!=groupList.end(); ++git) 00146 { 00147 kdDebug(7033) << *git << endl; 00148 00149 QStringList itemList = d->m_info.group(*git).preferredKeys(); 00150 if (itemList.isEmpty()) 00151 continue; 00152 00153 QGroupBox *groupBox = new QGroupBox(2, Qt::Horizontal, 00154 QStyleSheet::escape(mtinfo->groupInfo(*git)->translatedName()), 00155 d->m_frame); 00156 00157 toplayout->addWidget(groupBox); 00158 00159 QValueList<KFileMetaInfoItem> readItems; 00160 QValueList<KFileMetaInfoItem> editItems; 00161 00162 for (QStringList::Iterator iit = itemList.begin(); 00163 iit!=itemList.end(); ++iit) 00164 { 00165 KFileMetaInfoItem item = d->m_info[*git][*iit]; 00166 if ( !item.isValid() ) continue; 00167 00168 bool editable = file_info.isWritable() && item.isEditable(); 00169 00170 if (editable) 00171 editItems.append( item ); 00172 else 00173 readItems.append( item ); 00174 } 00175 00176 KFileMetaInfoWidget* w = 0L; 00177 // then first add the editable items to the layout 00178 for (QValueList<KFileMetaInfoItem>::Iterator iit= editItems.begin(); 00179 iit!=editItems.end(); ++iit) 00180 { 00181 QLabel* l = new QLabel((*iit).translatedKey() + ":", groupBox); 00182 l->setAlignment( AlignAuto | AlignTop | ExpandTabs ); 00183 QValidator* val = mtinfo->createValidator(*git, (*iit).key()); 00184 if (!val) kdDebug(7033) << "didn't get a validator for " << *git << "/" << (*iit).key() << endl; 00185 w = new KFileMetaInfoWidget(*iit, val, groupBox); 00186 d->m_editWidgets.append( w ); 00187 connect(w, SIGNAL(valueChanged(const QVariant&)), this, SIGNAL(changed())); 00188 } 00189 00190 // and then the read only items 00191 for (QValueList<KFileMetaInfoItem>::Iterator iit= readItems.begin(); 00192 iit!=readItems.end(); ++iit) 00193 { 00194 QLabel* l = new QLabel((*iit).translatedKey() + ":", groupBox); 00195 l->setAlignment( AlignAuto | AlignTop | ExpandTabs ); 00196 (new KFileMetaInfoWidget(*iit, KFileMetaInfoWidget::ReadOnly, 0L, groupBox)); 00197 } 00198 } 00199 00200 toplayout->addStretch(1); 00201 00202 // the add key (disabled until fully implemented) 00203 /* d->m_add = new QPushButton(i18n("&Add"), topframe); 00204 d->m_add->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, 00205 QSizePolicy::Fixed)); 00206 connect(d->m_add, SIGNAL(clicked()), this, SLOT(slotAdd())); 00207 tmp->addWidget(d->m_add); 00208 00209 // if nothing can be added, deactivate it 00210 if ( !d->m_info.supportsVariableKeys() ) 00211 { 00212 // if supportedKeys() does contain anything not in preferredKeys, 00213 // we have something addable 00214 00215 QStringList sk = d->m_info.supportedKeys(); 00216 d->m_add->setEnabled(false); 00217 for (QStringList::Iterator it = sk.begin(); it!=sk.end(); ++it) 00218 { 00219 if ( l.find(*it)==l.end() ) 00220 { 00221 d->m_add->setEnabled(true); 00222 kdDebug(250) << "**first addable key is " << (*it).latin1() << "**" <<endl; 00223 break; 00224 } 00225 kdDebug(250) << "**already existing key is " << (*it).latin1() << "**" <<endl; 00226 } 00227 } */ 00228 } 00229 00230 /*void KFileMetaPropsPlugin::slotAdd() 00231 { 00232 // add a lineedit for the name 00233 00234 00235 00236 // insert the item in the list 00237 00238 }*/ 00239 00240 KFileMetaPropsPlugin::~KFileMetaPropsPlugin() 00241 { 00242 delete d; 00243 } 00244 00245 bool KFileMetaPropsPlugin::supports( KFileItemList _items ) 00246 { 00247 #ifdef _GNUC 00248 #warning TODO: Add support for more than one item 00249 #endif 00250 if (KExecPropsPlugin::supports(_items) || KURLPropsPlugin::supports(_items)) 00251 return false; // Having both is redundant. 00252 00253 bool metaDataEnabled = KGlobalSettings::showFilePreview(_items.first()->url()); 00254 return _items.count() == 1 && metaDataEnabled; 00255 } 00256 00257 void KFileMetaPropsPlugin::applyChanges() 00258 { 00259 kdDebug(250) << "applying changes" << endl; 00260 // insert the fields that changed into the info object 00261 00262 QPtrListIterator<KFileMetaInfoWidget> it( d->m_editWidgets ); 00263 KFileMetaInfoWidget* w; 00264 for (; (w = it.current()); ++it) w->apply(); 00265 d->m_info.applyChanges(properties->kurl().path()); 00266 } 00267 00268 #include "kmetaprops.moc"