KCoreAddons

kmessage.cpp
1 /*
2  This file is part of the KDE libraries
3 
4  SPDX-FileCopyrightText: 2006 MichaĆ«l Larouche <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-only
7 */
8 #include "kmessage.h"
9 
10 #include <iostream>
11 
12 class StaticMessageHandler
13 {
14 public:
15  StaticMessageHandler()
16  {
17  }
18  ~StaticMessageHandler()
19  {
20  delete m_handler;
21  }
22  StaticMessageHandler(const StaticMessageHandler &) = delete;
23  StaticMessageHandler &operator=(const StaticMessageHandler &) = delete;
24 
25  /* Sets the new message handler and deletes the old one */
26  void setHandler(KMessageHandler *handler)
27  {
28  delete m_handler;
29  m_handler = handler;
30  }
31  KMessageHandler *handler() const
32  {
33  return m_handler;
34  }
35 
36 protected:
37  KMessageHandler *m_handler = nullptr;
38 };
39 Q_GLOBAL_STATIC(StaticMessageHandler, s_messageHandler)
40 
41 static void internalMessageFallback(KMessage::MessageType messageType, const QString &text, const QString &caption)
42 {
43  QString prefix;
44  switch (messageType) {
45  case KMessage::Error:
46  prefix = QStringLiteral("ERROR: ");
47  break;
48  case KMessage::Fatal:
49  prefix = QStringLiteral("FATAL: ");
50  break;
52  prefix = QStringLiteral("INFORMATION: ");
53  break;
54  case KMessage::Sorry:
55  prefix = QStringLiteral("SORRY: ");
56  break;
57  case KMessage::Warning:
58  prefix = QStringLiteral("WARNING: ");
59  break;
60  }
61 
63 
64  if (!caption.isEmpty()) {
65  message += QLatin1Char('(') + caption + QLatin1Char(')');
66  }
67 
68  message += prefix + text;
69 
70  // Show a message to the developer to setup a KMessageHandler
71  std::cerr << "WARNING: Please setup an KMessageHandler with KMessage::setMessageHandler to display message propertly." << std::endl;
72  // Show message to stdout
73  std::cerr << qPrintable(message) << std::endl;
74 }
75 
77 {
78  // Delete old message handler.
79  s_messageHandler()->setHandler(handler);
80 }
81 
82 void KMessage::message(KMessage::MessageType messageType, const QString &text, const QString &caption)
83 {
84  // Use current message handler if available, else use stdout
85  if (s_messageHandler()->handler()) {
86  s_messageHandler()->handler()->message(messageType, text, caption);
87  } else {
88  internalMessageFallback(messageType, text, caption);
89  }
90 }
KCOREADDONS_EXPORT void message(KMessage::MessageType messageType, const QString &text, const QString &caption=QString())
Display a long message of a certain type.
Definition: kmessage.cpp:82
Information message.
Definition: kmessage.h:50
Display an informative message using a KMessageHandler.
Definition: kmessage.h:38
KCOREADDONS_EXPORT void setMessageHandler(KMessageHandler *handler)
Set the current KMessageHandler Note that this method takes ownership of the KMessageHandler.
Definition: kmessage.cpp:76
Error message.
Definition: kmessage.h:45
Sorry message.
Definition: kmessage.h:60
MessageType
Definition: kmessage.h:40
Warning message.
Definition: kmessage.h:55
Abstract class for KMessage handler.
Definition: kmessage.h:101
Fatal message.
Definition: kmessage.h:65
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Apr 18 2021 23:02:02 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.