1 | /* |
2 | Copyright 2009 Esben Mose Hansen <kde@mosehansen.dk> |
3 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or |
7 | (at your option) any later version. |
8 | |
9 | This program is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | GNU General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU General Public License along |
15 | with this program; if not, write to the Free Software Foundation, Inc., |
16 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
17 | |
18 | */ |
19 | |
20 | #include "clipcommandprocess.h" |
21 | |
22 | #include <KCharMacroExpander> |
23 | |
24 | #include "history.h" |
25 | #include "historystringitem.h" |
26 | #include "urlgrabber.h" |
27 | |
28 | ClipCommandProcess::ClipCommandProcess(const ClipAction& action, const ClipCommand& command, const QString& clip, History* history, const HistoryItem* original_item) : |
29 | KProcess(), |
30 | m_history(history), |
31 | m_historyItem(original_item), |
32 | m_newhistoryItem() |
33 | { |
34 | QHash<QChar,QString> map; |
35 | map.insert( 's', clip ); |
36 | |
37 | // support %u, %U (indicates url param(s)) and %f, %F (file param(s)) |
38 | map.insert( 'u', clip ); |
39 | map.insert( 'U', clip ); |
40 | map.insert( 'f', clip ); |
41 | map.insert( 'F', clip ); |
42 | |
43 | const QStringList matches = action.regExpMatches(); |
44 | // support only %0 and the first 9 matches... |
45 | const int numMatches = qMin(10, matches.count()); |
46 | for ( int i = 0; i < numMatches; ++i ) { |
47 | map.insert( QChar( '0' + i ), matches.at( i ) ); |
48 | } |
49 | |
50 | setOutputChannelMode(OnlyStdoutChannel); |
51 | setShellCommand(KMacroExpander::expandMacrosShellQuote( command.command, map ).trimmed()); |
52 | |
53 | connect(this, SIGNAL(finished(int,QProcess::ExitStatus)), SLOT(slotFinished(int,QProcess::ExitStatus))); |
54 | if (command.output != ClipCommand::IGNORE) { |
55 | connect(this, SIGNAL(readyRead()), SLOT(slotStdOutputAvailable())); |
56 | } |
57 | if (command.output != ClipCommand::REPLACE) { |
58 | m_historyItem = 0L; // Don't replace |
59 | } |
60 | |
61 | } |
62 | |
63 | void ClipCommandProcess::slotFinished(int /*exitCode*/, QProcess::ExitStatus /*newState*/) |
64 | { |
65 | if (m_history) { |
66 | // If an history item was provided, remove it so that the new item can replace it |
67 | if (m_historyItem) { |
68 | m_history->remove(m_historyItem); |
69 | } |
70 | if (!m_newhistoryItem.isEmpty()) { |
71 | m_history->insert(new HistoryStringItem(m_newhistoryItem)); |
72 | } |
73 | } |
74 | deleteLater(); |
75 | } |
76 | |
77 | void ClipCommandProcess::slotStdOutputAvailable() |
78 | { |
79 | m_newhistoryItem.append(QString::fromLocal8Bit(this->readAllStandardOutput().data())); |
80 | } |
81 | |
82 | |
83 | #include "clipcommandprocess.moc" |
84 | |