• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdenetwork API Reference
  • KDE Home
  • Contact Us
 

kopete/libkopete

  • sources
  • kde-4.14
  • kdenetwork
  • kopete
  • libkopete
kopetecommand.cpp
Go to the documentation of this file.
1 /*
2  kopetecommand.cpp - Command
3 
4  Copyright (c) 2003 by Jason Keirstead <jason@keirstead.org>
5  Copyright (c) 2005 by Michel Hermier <michel.hermier@wanadoo.fr>
6  Kopete (c) 2002-2005 by the Kopete developers <kopete-devel@kde.org>
7 
8  *************************************************************************
9  * *
10  * This library is free software; you can redistribute it and/or *
11  * modify it under the terms of the GNU Lesser General Public *
12  * License as published by the Free Software Foundation; either *
13  * version 2 of the License, or (at your option) any later version. *
14  * *
15  *************************************************************************
16 */
17 
18 #include "kopetecommand.h"
19 
20 #include "kopetecontact.h"
21 #include "kopetechatsessionmanager.h"
22 #include "kopeteview.h"
23 #include "kopeteuiglobal.h"
24 
25 #include <kauthorized.h>
26 #include <kdebug.h>
27 #include <kinputdialog.h>
28 #include <klocale.h>
29 #include <kicon.h>
30 #include <kmessagebox.h>
31 
32 #include <qstringlist.h>
33 
34 class Kopete::Command::Private
35 {
36 public:
37  QString command;
38  QString help;
39  QString formatString;
40  uint minArgs;
41  int maxArgs;
42  bool processing;
43  Kopete::Command::Types types;
44  QObject* parent;
45 };
46 
47 Kopete::Command::Command( QObject *parent, const QString &command, const char* handlerSlot,
48  const QString &help, Kopete::CommandHandler::CommandType type, const QString &formatString,
49  uint minArgs, int maxArgs, const KShortcut &cut, const QString &pix )
50  : KAction( KIcon(pix), command[0].toUpper() + command.right( command.length() - 1).toLower(), 0 )
51  , d(new Private)
52 {
53  setObjectName( command.toLower() + QString::fromLatin1("_command") );
54  setShortcut( cut );
55  d->parent = parent;
56  connect(this, SIGNAL(triggered(bool)),this, SLOT(slotAction()));
57  connect(parent,SIGNAL(destroyed()),this,SLOT(deleteLater()));
58  init( command, handlerSlot, help, type, formatString, minArgs, maxArgs );
59 }
60 
61 Kopete::Command::~Command()
62 {
63  delete d;
64 }
65 
66 void Kopete::Command::init( const QString &command, const char* slot, const QString &help,
67  Kopete::CommandHandler::CommandType type, const QString &formatString, uint minArgs, int maxArgs )
68 {
69  m_command = command;
70  m_help = help;
71  m_type = type;
72  m_formatString = formatString;
73  m_minArgs = minArgs;
74  m_maxArgs = maxArgs;
75  m_processing = false;
76 
77  if(m_type == Kopete::CommandHandler::Normal )
78  {
79  QObject::connect(this, SIGNAL(handleCommand(QString,Kopete::ChatSession*)),
80  d->parent, slot );
81  }
82 }
83 
84 void Kopete::Command::slotAction()
85 {
86  Kopete::ChatSession *manager = Kopete::ChatSessionManager::self()->activeView()->msgManager();
87 
88  QString args;
89  if( m_minArgs > 0 )
90  {
91  args = KInputDialog::getText( i18n("Enter Arguments"), i18n("Enter the arguments to %1:", m_command) );
92  if( args.isNull() )
93  return;
94  }
95 
96  processCommand( args, manager, true );
97 }
98 
99 void Kopete::Command::processCommand( const QString &args, Kopete::ChatSession *manager, bool gui )
100 {
101  QStringList mArgs = Kopete::CommandHandler::parseArguments( args );
102  if( m_processing )
103  {
104  printError( i18n("Alias \"%1\" expands to itself.", text() ), manager, gui );
105  }
106  else if( mArgs.count() < m_minArgs )
107  {
108  printError( i18np("\"%2\" requires at least %1 argument.",
109  "\"%2\" requires at least %1 arguments.", m_minArgs,
110  text() ), manager, gui );
111  }
112  else if( m_maxArgs > -1 && (int)mArgs.count() > m_maxArgs )
113  {
114  printError( i18np("\"%2\" has a maximum of %1 argument.",
115  "\"%2\" has a maximum of %1 arguments.", m_minArgs,
116  text() ), manager, gui );
117  }
118  else if( !KAuthorized::authorizeKAction( objectName() ) )
119  {
120  printError( i18n("You are not authorized to perform the command \"%1\".", text()), manager, gui );
121  }
122  else
123  {
124  m_processing = true;
125  if( m_type == Kopete::CommandHandler::UserAlias ||
126  m_type == Kopete::CommandHandler::SystemAlias )
127  {
128  QString formatString = m_formatString;
129 
130  // Translate %s to the whole string and %n to current display name
131 
132  formatString.replace( QString::fromLatin1("%n"), manager->myself()->displayName() );
133  formatString.replace( QString::fromLatin1("%s"), args );
134 
135  // Translate %1..%N to word1..wordN
136  while( mArgs.count() > 0 )
137  {
138  formatString = formatString.arg( mArgs.front() );
139  mArgs.pop_front();
140  }
141 
142  kDebug(14010) << "New Command after processing alias: " << formatString;
143 
144  Kopete::CommandHandler::commandHandler()->processMessage( QString::fromLatin1("/") + formatString, manager );
145  }
146  else
147  {
148  emit( handleCommand( args, manager ) );
149  }
150  m_processing = false;
151  }
152 }
153 
154 void Kopete::Command::printError( const QString &error, Kopete::ChatSession *manager, bool gui ) const
155 {
156  if( gui )
157  {
158  KMessageBox::error( Kopete::UI::Global::mainWidget(), error, i18n("Command Error") );
159  }
160  else
161  {
162  Kopete::Message msg( manager->myself(), manager->members() );
163  msg.setPlainBody(error);
164  msg.setDirection( Kopete::Message::Internal );
165 
166  manager->appendMessage( msg );
167  }
168 }
169 
170 #include "kopetecommand.moc"
Kopete::CommandHandler::processMessage
bool processMessage(Message &msg, ChatSession *manager)
Process a message to see if any commands should be handled.
Definition: kopetecommandhandler.cpp:239
Kopete::CommandHandler::CommandType
CommandType
an enum defining the type of a command
Definition: kopetecommandhandler.h:58
Kopete::ChatSessionManager::activeView
KopeteView * activeView()
Returns the current active Kopete view.
Definition: kopetechatsessionmanager.cpp:187
kopetechatsessionmanager.h
Kopete::Command::Command
Command(QObject *parent, const QString &command, const char *handlerSlot, const QString &help=QString(), CommandHandler::CommandType type=CommandHandler::Normal, const QString &formatString=QString(), uint minArgs=0, int maxArgs=-1, const KShortcut &cut=KShortcut(), const QString &pix=QString())
Creates a Kopete::Command object.
Definition: kopetecommand.cpp:47
KopeteView::msgManager
Kopete::ChatSession * msgManager() const
Get the message manager.
Definition: kopeteview.cpp:26
Kopete::Command::~Command
~Command()
Definition: kopetecommand.cpp:61
Kopete::Command::command
const QString & command() const
Returns the command this object handles.
Definition: kopetecommand.h:81
Kopete::CommandHandler::SystemAlias
Definition: kopetecommandhandler.h:58
Kopete::ChatSession
Definition: kopetechatsession.h:74
Kopete::CommandHandler::parseArguments
static QStringList parseArguments(const QString &args)
Parses a string of command arguments into a QStringList.
Definition: kopetecommandhandler.cpp:429
kopeteuiglobal.h
Kopete::Message::Internal
(Default) Message which are not sent via the network. This is just a notification a plugin can show i...
Definition: kopetemessage.h:92
QString::isNull
bool isNull() const
Kopete::ChatSession::appendMessage
void appendMessage(Kopete::Message &msg)
Show a message to the chatwindow, or append it to the queue.
Definition: kopetechatsession.cpp:310
QList::count
int count(const T &value) const
QList::pop_front
void pop_front()
QObject
Kopete::Command::processCommand
void processCommand(const QString &args, ChatSession *manager, bool gui=false)
Process this command.
Definition: kopetecommand.cpp:99
QList::front
T & front()
Kopete::Message::setPlainBody
void setPlainBody(const QString &body)
Sets the body of the message.
Definition: kopetemessage.cpp:234
QString
QStringList
QString::toLower
QString toLower() const
Kopete::Command::help
const QString & help() const
Returns the help string for this command.
Definition: kopetecommand.h:86
Kopete::UI::Global::mainWidget
KOPETE_EXPORT QWidget * mainWidget()
Returns the main widget - this is the widget that message boxes and KNotify stuff should use as a par...
Definition: kopeteuiglobal.cpp:37
Kopete::ChatSessionManager::self
static ChatSessionManager * self()
Definition: kopetechatsessionmanager.cpp:39
QString::replace
QString & replace(int position, int n, QChar after)
Kopete::ChatSession::myself
const Contact * myself() const
Get the local user in the session.
Definition: kopetechatsession.cpp:215
Kopete::ChatSession::members
const ContactPtrList & members() const
Get a list of all contacts in the session.
Definition: kopetechatsession.cpp:210
KAction
Kopete::CommandHandler::UserAlias
Definition: kopetecommandhandler.h:58
Kopete::Contact::displayName
QString displayName() const
Returns display name of contact.
Definition: kopetecontact.cpp:913
QString::fromLatin1
QString fromLatin1(const char *str, int size)
kopeteview.h
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
Kopete::CommandHandler::commandHandler
static CommandHandler * commandHandler()
Returns a pointer to the command handler.
Definition: kopetecommandhandler.cpp:164
Kopete::Message
Representation of a message in Kopete.
Definition: kopetemessage.h:82
kopetecontact.h
Kopete::CommandHandler::Normal
Definition: kopetecommandhandler.h:58
kopetecommand.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:29:19 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kopete/libkopete

Skip menu "kopete/libkopete"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal