1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
//
// C++ Implementation: filenameview
//
// Description:
//
//
// Author: Benjamin Mesing <bensmail@gmx.net>, (C) 2005
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "filenameview.h"
#include <assert.h>
#include <qapplication.h>
#include <qclipboard.h>
#include <qevent.h>
#include <qfileinfo.h>
#include <QMenu>
#include <qstatusbar.h>
#include <iprovider.h>
QString FilenameView::_seeCommand("/usr/bin/see");
FilenameView::FilenameView(QWidget *parent, const char *name, NPlugin::IProvider* pProvider)
: QWidget(parent), Ui::FilenameView()
{
setObjectName(name);
_filterTextEmpty = true;
_pProvider = pProvider;
setupUi(this);
_pErrorDisplay->setVisible(false);
connect(_pShowButton, SIGNAL(clicked()), SIGNAL(showRequested()));
connect( &_processContainer, SIGNAL(processExited(QProcess*)), this, SLOT(onProcessExited(QProcess*)) );
}
FilenameView::~FilenameView()
{
}
void FilenameView::viewFile(QString filename)
{
_pProvider->statusBar()->showMessage(tr("Trying to view ") + filename, 4000);
QFileInfo seeInfo(_seeCommand );
if ( !seeInfo.isExecutable() )
{
_pProvider->reportError(
_seeCommand + tr(" not available"),
tr("The <tt>") + _seeCommand + tr("</tt> command is not available.\n"
"Please make sure that the <tt>mime-support</tt> package is installed "
"and that you have permission to execute <tt>") + _seeCommand + tr("</tt>.")
);
return;
}
QProcess* pProcess = new QProcess(this);
QStringList arguments;
arguments.push_back(filename);
_seeCommands[pProcess] = make_pair(filename, false);
if (!_processContainer.start(pProcess, _seeCommand , arguments))
{
_pProvider->reportError
(
tr("Unable to launch ") + _seeCommand ,
tr("Launching <tt>") + _seeCommand + " " + filename + tr("</tt> failed due to an unknown reason.")
);
}
}
void FilenameView::onProcessExited(QProcess* pProcess )
{
static const QString TEXT_PLAIN_PREFIX("text/plain:");
qDebug("process exited");
// if the "see" did not exit normally try again specifying mimetime text/plain
pair<QString, bool> seeInformation = _seeCommands[pProcess];
QString filename = seeInformation.first;
// Holds if viewing the file using mimetype text/plain was already tried
bool wasTriedAsTextPlain = seeInformation.second;
if (pProcess->exitCode() != 0)
{
qDebug("non-normal exit");
if (wasTriedAsTextPlain)
{
_seeCommands.erase(pProcess);
pProcess->deleteLater();
_pProvider->reportError( tr("Error viewing file"), tr("Unable to view file ") + filename
+ tr("\nTried <tt>") + _seeCommand + " " + filename + tr("</tt> and\n<tt>") + _seeCommand + " " +
TEXT_PLAIN_PREFIX + filename + "</tt>" );
// qDebug("Error launching see " + pItem->text(0));
}
else
{
_seeCommands[pProcess] = make_pair(filename, true);
_pProvider->statusBar()->showMessage(tr("Retrying to view ") + filename + tr(" with mimetype text/plain"),
4000);
qDebug("%s", (QString("retrying process ") + filename).toLatin1().data());
QStringList arguments;
arguments.push_back(TEXT_PLAIN_PREFIX+filename);
_processContainer.start(pProcess, "see", arguments);
}
}
else
{
qDebug("normal exit");
_pProvider->statusBar()->showMessage(tr("Finished viewing ") + filename, 4000);
_seeCommands.erase(pProcess);
pProcess->deleteLater();
}
}
void FilenameView::on__pFilenameView_itemDoubleClicked(QListWidgetItem* pItem)
{
if (pItem == 0)
{
qDebug("Didn't hit an item");
return;
}
QString filename = pItem->text();
if (!isFileViewable(filename))
{
_pProvider->statusBar()->showMessage(tr("Can't view file ") + filename + tr(", it is not viewable"),
5000);
return;
}
viewFile(pItem->text());
}
void FilenameView::on__pFilterInput_textChanged( const QString & pattern )
{
_filterTextEmpty = pattern.isEmpty();
updateView();
}
void FilenameView::on__pFilenameView_customContextMenuRequested(const QPoint& pos)
{
QListWidgetItem* pItem = _pFilenameView->currentItem();
// if no item was selected return immidiately
if (pItem == 0)
return;
QMenu menu(this);
QString filename = pItem->text();
QAction* pCopyToClipboard = menu.addAction(tr("Copy to clipboard"));
QAction* pCopyAllToClipboard = menu.addAction(tr("Copy all filenames to clipboard"));
QAction* pViewFile = menu.addAction(tr("View file (depends on settings in /etc/mailcap)"));
if (!isFileViewable(filename))
pViewFile->setEnabled(false);
QAction* pResult = menu.exec(_pFilenameView->mapToGlobal(pos));
if (pResult == pCopyToClipboard)
{
QClipboard *pCb = QApplication::clipboard();
pCb->setText(filename, QClipboard::Clipboard);
pCb->setText(filename, QClipboard::Selection);
}
else if (pResult == pCopyAllToClipboard)
{
QClipboard *pCb = QApplication::clipboard();
QString itemsString = getAllVisibleItems().join("\n");
pCb->setText(itemsString, QClipboard::Clipboard);
pCb->setText(itemsString, QClipboard::Selection);
}
else if (pResult == pViewFile)
{
viewFile(filename);
}
}
void FilenameView::clear()
{
_errorMessage = "";
_entries.clear();
_pFilenameView->clear();
}
void FilenameView::setFilterText( const QString & pattern )
{
_pFilterInput->setText(pattern);
}
void FilenameView::addEntry( const QString & entry )
{
_entries.push_back(entry);
insertItem(entry);
}
void FilenameView::updateView()
{
_pFilenameView->clear();
if (_errorMessage.isEmpty()) // if no error was indicated
{
_pFilenameView->setVisible(true);
_pErrorDisplay->setVisible(false);
for ( QStringList::iterator it = _entries.begin(); it != _entries.end(); ++it)
{
insertItem(*it);
}
}
else
{
_pErrorDisplay->setHtml(_errorMessage);
_pFilenameView->setVisible(false);
_pErrorDisplay->setVisible(true);
}
}
void FilenameView::setErrorMessage( const QString & errorMessage )
{
/** The error message holds the error state. If it is empty no error has occured. */
_errorMessage = errorMessage;
updateView();
}
void FilenameView::insertItem( const QString & entry )
{
if ( _filterTextEmpty || entry.contains(_pFilterInput->text()) )
{
new QListWidgetItem(entry, _pFilenameView);
}
_pFilenameView->setVisible(true);
_pErrorDisplay->setVisible(false);
}
QStringList FilenameView::getAllVisibleItems()
{
QStringList result;
if (_errorMessage.isEmpty())
{
for ( int i = 0; i < _pFilenameView->count(); ++i)
{
QListWidgetItem* pItem = _pFilenameView->item(i);
if (!pItem->isHidden())
result.push_back(pItem->text());
}
}
return result;
}
/** @brief Returns if the file is viewable (i.e. no directory and readable). */
bool FilenameView::isFileViewable(QString filename)
{
QFileInfo fileinfo(filename);
return fileinfo.isReadable() && !fileinfo.isDir();
}
void FilenameView::setShowButtonEnabled(bool enabled)
{
_pShowButton->setEnabled(enabled);
}
|