summaryrefslogtreecommitdiffstats
path: root/tderesources/egroupware/debugdialog.cpp
blob: 70cc87f57ed94a415ae407afa6a56685ae005944 (plain)
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
/*
    This file is part of tdepim.
    Copyright (c) 2005 Tobias Koenig <[email protected]>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include <tqfile.h>
#include <tqfiledialog.h>
#include <tqlayout.h>

#include <ktextbrowser.h>
#include <kstaticdeleter.h>

#include <stdlib.h>
#include <tdelocale.h>
#include "debugdialog.h"

DebugDialog* DebugDialog::mSelf = 0;
static KStaticDeleter<DebugDialog> debugDialogDeleter;

DebugDialog::DebugDialog()
 : KDialogBase( Plain, WStyle_DialogBorder | WStyle_StaysOnTop, 0,
                "Debug Dialog", false, i18n("Debug Dialog"),
                User1 | User2 | Ok, Ok, true )
{
  TQWidget *page = plainPage();
  TQVBoxLayout *layout = new TQVBoxLayout( page, marginHint(), spacingHint() );

  mView = new KTextBrowser( page );
  layout->addWidget( mView );

  setButtonText( User1, "Save As..." );
  setButtonText( User2, "Clear" );

  clear();
}

void DebugDialog::init()
{
  if ( !mSelf ) {
    if ( getenv( "EGROUPWARE_DEBUG" ) != 0 ) {
      debugDialogDeleter.setObject( mSelf, new DebugDialog );
    }
  }

  if ( mSelf ) {
    mSelf->show();
    mSelf->raise();
  }
}

DebugDialog::~DebugDialog()
{
  mSelf = 0;
}

void DebugDialog::addMessage( const TQString &msg, Type type )
{
  if ( mSelf )
    mSelf->addText( msg, type );
}

void DebugDialog::clear()
{
  mView->clear();
  mMessages.clear();
}

void DebugDialog::save()
{
  TQString fileName = TQFileDialog::getSaveFileName();
  if ( fileName.isEmpty() )
    return;

  TQFile file( fileName );
  if ( !file.open( IO_WriteOnly ) ) {
    tqWarning( "Couldn't open file %s", file.name().latin1() );
    return;
  }

  file.writeBlock( mMessages.join( "\n\n" ).utf8() );
  file.close();
}

void DebugDialog::slotUser1()
{
  save();
}

void DebugDialog::slotUser2()
{
  clear();
}

void DebugDialog::addText( const TQString &text, Type type )
{
  TQString htmlCode( text );
  htmlCode.replace( "<", "&lt;" );
  htmlCode.replace( ">", "&gt;" );
  htmlCode.replace( "\n", "<br>" );

  mMessages.append( text );
  if ( type == Input )
    mHTMLMessages.append( "<font color=\"green\">" + htmlCode + "</font>" );
  else
    mHTMLMessages.append( "<font color=\"blue\">" + htmlCode + "</font>" );

  mView->clear();
  mView->setText( mHTMLMessages.join( "<br>" ) );
}

#include "debugdialog.moc"