/*************************************************************************** * Copyright (C) 2005 by Florian Roth * * florian@synatic.net * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "defaultplugin.h" #include "metabarwidget.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define EVENT_TYPE DOM::DOMString("click") #define MODIFICATION 1 DefaultPlugin::DefaultPlugin(KHTMLPart* html, MetabarFunctions *functions, const char *name) : ProtocolPlugin (html, functions, name) { services = new ServiceLoader(m_html->view(), "serviceloader"); preview_job = 0; } DefaultPlugin::~DefaultPlugin() { } void DefaultPlugin::killJobs(){ if(preview_job){ preview_job->kill(); preview_job = 0; } } void DefaultPlugin::deactivate() { m_functions->hide("actions"); m_functions->hide("open"); m_functions->hide("info"); m_functions->hide("preview"); } void DefaultPlugin::loadActions(DOM::HTMLElement node) { KFileItem *item = m_items.getFirst(); KURL url = item->url(); KConfig config("metabarrc", true, false); config.setGroup("General"); KConfig iconConfig(locate("data", "metabar/iconsrc")); iconConfig.setGroup("Icons"); DOM::DOMString innerHTML; QStringList actions = config.readListEntry("Actions"); int maxActions = config.readNumEntry("MaxActions", 5); int actionCount = 0; for(QStringList::Iterator it = actions.begin(); it != actions.end(); ++it){ if((*it).startsWith("metabar/")){ if((*it).right((*it).length() - 8) == "share"){ MetabarWidget::addEntry(innerHTML, i18n("Share"), "action://" + *it, "network", QString::null, actionCount < maxActions ? QString::null : QString("hiddenaction"), actionCount >= maxActions); actionCount++; } } else{ DCOPRef action(kapp->dcopClient()->appId(), QCString(m_html->view()->topLevelWidget()->name()).append("/action/").append((*it).utf8())); if(!action.isNull()){ if(action.call("enabled()")){ QString text = action.call("plainText()"); QString icon = iconConfig.readEntry(*it, action.call("icon()")); MetabarWidget::addEntry(innerHTML, text, "action://" + *it, icon, QString::null, actionCount < maxActions ? QString::null : QString("hiddenaction"), actionCount >= maxActions); actionCount++; } } } } config.setGroup("General"); if(config.readBoolEntry("ShowServicemenus", true)){ services->loadServices(*(m_items.getFirst()), innerHTML, actionCount); } if(actionCount == 0) { m_functions->hide("actions"); } else{ if(actionCount > maxActions){ MetabarWidget::addEntry(innerHTML, i18n("More"), "more://hiddenaction", "1downarrow", "hiddenaction"); } node.setInnerHTML(innerHTML); m_functions->show("actions"); } } void DefaultPlugin::loadApplications(DOM::HTMLElement node) { if(m_items.count() == 1){ KFileItem *item = m_items.getFirst(); KURL url = item->url(); QDir dir(url.path()); dir = dir.canonicalPath(); if(item->isDir() || dir.isRoot()){ m_functions->hide("open"); node.setInnerHTML(DOM::DOMString()); } else{ DOM::DOMString innerHTML; if(KDesktopFile::isDesktopFile(item->url().path())){ //Desktop file KDesktopFile desktop(url.path(), TRUE); if(desktop.hasApplicationType ()){ MetabarWidget::addEntry(innerHTML, i18n("Run %1").arg(desktop.readName()), "desktop://" + url.path(), desktop.readIcon()); m_functions->show("open"); } else{ m_functions->hide("open"); } } else{ //other files KTrader::OfferList offers; offers = KTrader::self()->query(item->mimetype(), "Type == 'Application'"); if(!offers.isEmpty()){ KConfig config("metabarrc", true, false); config.setGroup("General"); int id = 0; int max = config.readNumEntry("MaxEntries", 5); runMap.clear(); KTrader::OfferList::ConstIterator it = offers.begin(); for(; it != offers.end(); it++){ QString nam; nam.setNum(id); bool hide = id >= max; MetabarWidget::addEntry(innerHTML, (*it)->name(), "exec://" + nam, (*it)->icon(), QString::null, hide ? QString("hiddenapp") : QString::null, hide); runMap.insert(id, *it); id++; } if(id > max){ MetabarWidget::addEntry(innerHTML, i18n("More"), "more://hiddenapp", "1downarrow", "hiddenapp"); } } else{ MetabarWidget::addEntry(innerHTML, i18n("Choose Application"), "openwith:///", "run"); } } node.setInnerHTML(innerHTML); m_functions->show("open"); } } else{ m_functions->hide("open"); } } void DefaultPlugin::loadInformation(DOM::HTMLElement node) { if(m_items.count() == 1){ KFileItem *item = m_items.getFirst(); KFileItem *that = const_cast(item); DOM::DOMString innerHTML; innerHTML += "
    " + i18n("Type") + ": "; innerHTML += that->determineMimeType()->comment(); innerHTML += "
    " + i18n("Size") + ": "; innerHTML += KIO::convertSize(item->size()); innerHTML += "
    " + i18n("User") + ": "; innerHTML += item->user(); innerHTML += "
    " + i18n("Group") + ": "; innerHTML += item->group(); innerHTML += "
    " + i18n("Permissions") + ": "; innerHTML += (item->permissionsString()); innerHTML += "
    " + i18n("Modified") + ": "; innerHTML += item->timeString(KIO::UDS_MODIFICATION_TIME); innerHTML += "
    " + i18n("Accessed") + ": "; innerHTML += item->timeString(KIO::UDS_ACCESS_TIME); innerHTML += "
"; if(item->isLink()){ innerHTML += "
    " + i18n("Linktarget") + ": "; innerHTML += item->linkDest(); innerHTML += "
"; } if(!item->isDir()){ const KFileMetaInfo &metaInfo = item->metaInfo(); if(metaInfo.isValid()){ QStringList groups = metaInfo.supportedGroups(); int id = 0; QString nam; for(QStringList::ConstIterator it = groups.begin(); it != groups.end(); ++it){ KFileMetaInfoGroup group = metaInfo.group(*it); if(group.isValid()){ nam.setNum(id); innerHTML += ""; QStringList keys = group.supportedKeys(); for(QStringList::ConstIterator it = keys.begin(); it != keys.end(); ++it){ const KFileMetaInfoItem metaInfoItem = group.item(*it); if(metaInfoItem.isValid()){ innerHTML += "
    " + metaInfoItem.translatedKey() + ": "; innerHTML += metaInfoItem.string(); innerHTML += "
"; } } id++; } } } } node.setInnerHTML(innerHTML); } else{ KIO::filesize_t size = 0; int files = 0; int dirs = 0; for(KFileItemListIterator it(m_items); it.current(); ++it){ size += (*it)->size(); if((*it)->isDir()){ dirs++; } else{ files++; } } DOM::DOMString innerHTML; innerHTML += "
    " + i18n("Size") + ": "; innerHTML += KIO::convertSize(size); innerHTML += "
    " + i18n("Files") + ": "; innerHTML += QString().setNum(files); innerHTML += "
    " + i18n("Folders") + ": "; innerHTML += QString().setNum(dirs); innerHTML += "
    " + i18n("Total Entries") + ": "; innerHTML += QString().setNum(m_items.count()); innerHTML += "
"; node.setInnerHTML(innerHTML); } m_functions->show("info"); } void DefaultPlugin::loadPreview(DOM::HTMLElement node) { if(m_items.count() == 1){ KFileItem *item = m_items.getFirst(); KURL url = item->url(); QDir dir(url.path()); dir = dir.canonicalPath(); if(item->isDir() || dir.isRoot()){ m_functions->hide("preview"); } else{ if(item->mimetype().startsWith("audio/")){ DOM::DOMString innerHTML(""; node.setInnerHTML(innerHTML); //m_functions->show("preview"); } else{ DOM::DOMString innerHTML("
    "); innerHTML += i18n("Creating preview"); innerHTML += "
"; node.setInnerHTML(innerHTML); //m_functions->show("preview"); preview_job = KIO::filePreview(KURL::List(url), m_html->view()->width() - 30); connect(preview_job, SIGNAL(gotPreview(const KFileItem*, const QPixmap&)), this, SLOT(slotSetPreview(const KFileItem*, const QPixmap&))); connect(preview_job, SIGNAL(failed(const KFileItem *)), this, SLOT(slotPreviewFailed(const KFileItem *))); connect(preview_job, SIGNAL(result(KIO::Job *)), this, SLOT(slotJobFinished(KIO::Job *))); } m_functions->show("preview"); } } else{ m_functions->hide("preview"); } } void DefaultPlugin::loadBookmarks(DOM::HTMLElement node) { m_functions->hide("bookmarks"); } void DefaultPlugin::slotSetPreview(const KFileItem *item, const QPixmap &pix) { DOM::HTMLDocument doc = m_html->htmlDocument(); DOM::HTMLElement node = doc.getElementById("preview"); QByteArray data; QBuffer buffer(data); buffer.open(IO_WriteOnly); pix.save(&buffer, "PNG"); QString src = QString::fromLatin1("data:image/png;base64,%1").arg(KCodecs::base64Encode(data)); bool media = item->mimetype().startsWith("video/"); DOM::DOMString innerHTML; innerHTML += QString("
"; if(media){ innerHTML += ""; } node.setInnerHTML(innerHTML); //script.append("adjustPreviewSize(" + height_str + ");"); m_functions->show("preview"); m_functions->adjustSize("preview"); } void DefaultPlugin::slotPreviewFailed(const KFileItem *item) { DOM::HTMLDocument doc = m_html->htmlDocument(); DOM::HTMLElement preview = static_cast(doc.getElementById("preview_image")); if(!preview.isNull()){ preview.setAttribute(DOM::DOMString("src"), DOM::DOMString()); } m_functions->hide("preview"); } void DefaultPlugin::slotJobFinished(KIO::Job *job) { if(preview_job && job == preview_job){ preview_job = 0; } } bool DefaultPlugin::handleRequest(const KURL &url) { QString protocol = url.protocol(); if(protocol == "exec"){ int id = url.host().toInt(); QMap::Iterator it = runMap.find(id); if(it != runMap.end()){ KFileItem *item = m_items.getFirst(); if(item){ KRun::run( **it, KURL::List(item->url())); return true; } } } else if(protocol == "service"){ QString name = url.url().right(url.url().length() - 10); services->runAction(name); return true; } else if(protocol == "servicepopup"){ QString id = url.host(); DOM::HTMLDocument doc = m_html->htmlDocument(); DOM::HTMLElement node = static_cast(doc.getElementById("popup" + id)); if(!node.isNull()){ QRect rect = node.getRect(); QPoint p = m_html->view()->mapToGlobal(rect.bottomLeft()); services->showPopup(id, p); } return true; } return false; } #include "defaultplugin.moc"