KDECore
kmessage.cpp
Go to the documentation of this file.00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2006 Michaƫl Larouche <michael.larouche@kdemail.net> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; version 2 00007 of the License. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 #include "kmessage.h" 00020 00021 #include <kglobal.h> 00022 00023 #include <QtCore/QLatin1String> 00024 00025 #include <iostream> 00026 00027 class StaticMessageHandler 00028 { 00029 public: 00030 StaticMessageHandler() : m_handler(0) {} 00031 ~StaticMessageHandler() 00032 { 00033 delete m_handler; 00034 } 00035 /* Sets the new message handler and deletes the old one */ 00036 void setHandler(KMessageHandler *handler) 00037 { 00038 delete m_handler; 00039 m_handler = handler; 00040 } 00041 KMessageHandler *handler() const 00042 { 00043 return m_handler; 00044 } 00045 00046 protected: 00047 KMessageHandler *m_handler; 00048 }; 00049 K_GLOBAL_STATIC(StaticMessageHandler, s_messageHandler) 00050 00051 static void internalMessageFallback(KMessage::MessageType messageType, const QString &text, const QString &caption) 00052 { 00053 QString prefix; 00054 switch(messageType) 00055 { 00056 case KMessage::Error: 00057 prefix = QLatin1String("ERROR: "); 00058 break; 00059 case KMessage::Fatal: 00060 prefix = QLatin1String("FATAL: "); 00061 break; 00062 case KMessage::Information: 00063 prefix = QLatin1String("INFORMATION: "); 00064 break; 00065 case KMessage::Sorry: 00066 prefix = QLatin1String("SORRY: "); 00067 break; 00068 case KMessage::Warning: 00069 prefix = QLatin1String("WARNING: "); 00070 break; 00071 } 00072 00073 QString message; 00074 00075 if( !caption.isEmpty() ) 00076 message += '(' + caption + ')'; 00077 00078 message += prefix + text; 00079 00080 // Show a message to the developer to setup a KMessageHandler 00081 std::cerr << "WARNING: Please setup an KMessageHandler with KMessage::setMessageHandler to display message propertly." << std::endl; 00082 // Show message to stdout 00083 std::cerr << qPrintable(message) << std::endl; 00084 } 00085 00086 void KMessage::setMessageHandler(KMessageHandler *handler) 00087 { 00088 // Delete old message handler. 00089 s_messageHandler->setHandler(handler); 00090 } 00091 00092 void KMessage::message(KMessage::MessageType messageType, const QString &text, const QString &caption) 00093 { 00094 // Use current message handler if available, else use stdout 00095 if(s_messageHandler->handler()) 00096 { 00097 s_messageHandler->handler()->message(messageType, text, caption); 00098 } 00099 else 00100 { 00101 internalMessageFallback(messageType, text, caption); 00102 } 00103 } 00104 00105 // kate: space-indent on; indent-width 4; encoding utf-8; replace-tabs on;