summaryrefslogtreecommitdiffstats
path: root/kommander/editor/messagelog.cpp
blob: cf8bdd25410b81f01a01f005b2e6ac61a57b5863 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/***************************************************************************
                    messagelog.cpp - Kommander dialog output
                             -------------------
    copyright          : (C) 2004      Michal Rudolf <mrudolf@kdewebdwev.org>
    
 ***************************************************************************/

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

#include "messagelog.h"

#include <tdeapplication.h>
#include <tdefiledialog.h>
#include <kiconloader.h>
#include <tdelistbox.h>
#include <tdemessagebox.h>
#include <tdepopupmenu.h>
#include <kprocess.h>

#include <tqclipboard.h>
#include <tqfile.h>
#include <tqtextstream.h>

MessageLog::MessageLog(TQWidget* parent, const char* name) : TQTabWidget(parent, name)
{
  m_popupMenu = new TDEPopupMenu(this);
  m_popupMenu->insertItem(SmallIconSet("edit-copy"), i18n("Copy Current &Line"), this, TQ_SLOT(copyLine()));
  m_popupMenu->insertItem(SmallIconSet("edit-copy"), i18n("&Copy Content"), this, TQ_SLOT(copyContent()));
  m_popupMenu->insertItem(SmallIconSet("document-save-as"), i18n("&Save As..."), this, TQ_SLOT(saveToFile()));
  m_popupMenu->insertSeparator();
  m_popupMenu->insertItem(SmallIconSet("edit-clear"), i18n("Clear"), this, TQ_SLOT(clearContent()));
  
  for (int i = 0; i < m_listCount; i++)
  {
    m_lists[i] = new TDEListBox(this);
    addTab(m_lists[i], m_listNames[i]);
    m_seenEOL[i] = false;
    connect(m_lists[i], TQ_SIGNAL(contextMenuRequested(TQListBoxItem*, const TQPoint&)),
            this, TQ_SLOT(showMenu(TQListBoxItem*, const TQPoint&)));
  }
}
  
MessageLog::~MessageLog()
{
}
  
void MessageLog::insertItem(InfoType i, TQString text)
{
  bool seenEOL = text.endsWith("\n");
  if (seenEOL)
    text.truncate(text.length() - 1);
  TQStringList items(TQStringList::split('\n', text));
  for (TQStringList::ConstIterator it = items.begin(); it != items.end(); ++it ) 
  {
    if (!m_seenEOL[i] && m_lists[i]->count() && it == items.begin())
      m_lists[i]->changeItem(m_lists[i]->text(m_lists[i]->count() - 1) + *it, m_lists[i]->count() - 1);
    else
      m_lists[i]->insertItem(*it);
  }
  m_seenEOL[i] = seenEOL;
  m_lists[i]->setCurrentItem(m_lists[i]->count()-1);
  m_lists[i]->ensureCurrentVisible();
}

TQString MessageLog::content()
{
  TQString p_content;
  TDEListBox* list = m_lists[currentPageIndex()];
  for (uint i=0; i < list->count(); i++)
    p_content.append(list->text(i) + "\n");
  return p_content;
}

void MessageLog::clear(InfoType i)
{
  if (i != All)
  {
    m_lists[(int)i]->clear(); 
    m_seenEOL[i] = false;
  }
  else 
    for (int i = 0; i < m_listCount; i++)
      clear((InfoType)i);
}

void MessageLog::receivedStdout(TDEProcess*, char* buffer, int buflen)
{
  insertItem(Stdout, TQString::fromLocal8Bit(buffer, buflen));
}

void MessageLog::receivedStderr(TDEProcess*, char* buffer, int buflen)
{
  insertItem(Stderr, TQString::fromLocal8Bit(buffer, buflen));
}

void MessageLog::clearContent()
{
  clear((InfoType)currentPageIndex()); 
}

void MessageLog::copyLine()
{
  if (m_lists[currentPageIndex()]->count())
    kapp->clipboard()->setText(m_lists[currentPageIndex()]->currentText(), TQClipboard::Clipboard);
}

void MessageLog::copyContent()
{
  kapp->clipboard()->setText(content(), TQClipboard::Clipboard);
}

void MessageLog::saveToFile()
{
  KURL url=KFileDialog::getSaveURL(TQDir::currentDirPath(),
                                   i18n("*.log|Log Files (*.log)\n*|All Files"), this, i18n("Save Log File"));
  if (url.isEmpty())
    return;
  TQFileInfo fileinfo(url.path());
  if (fileinfo.exists() && KMessageBox::warningContinueCancel(0,
      i18n("<qt>File<br><b>%1</b><br>already exists. Overwrite it?</qt>")
          .arg(url.path()), TQString(), i18n("Overwrite")) == KMessageBox::Cancel)
    return;
  TQFile file(url.path());
  if (!file.open(IO_WriteOnly)) {
    KMessageBox::error(0, i18n("<qt>Cannot save log file<br><b>%1</b></qt>")
        .arg(url.url()));
    return;
  }
  TQTextStream textfile(&file);
  textfile << content();
  file.close();
}

void MessageLog::showMenu(TQListBoxItem*, const TQPoint& l_point)
{
  m_popupMenu->exec(l_point);
}

TQString MessageLog::m_listNames[m_listCount] = {i18n("Stdout"), i18n("Stderr")};

#include "messagelog.moc"