summaryrefslogtreecommitdiffstats
path: root/kate/app/kateconsole.cpp
blob: 1e87a59d81ac2fe10d6a9512e9ef8820df87a481 (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
/* This file is part of the KDE project
   Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org>
   Copyright (C) 2002 Joseph Wenninger <jowenn@kde.org>
   Copyright (C) 2002 Anders Lund <anders.lund@lund.tdcadsl.dk>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   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 "kateconsole.h"
#include "kateconsole.moc"

#include "katemain.h"
#include "katemdi.h"
#include "katemainwindow.h"
#include "kateviewmanager.h"

#include <kate/view.h>
#include <kate/document.h>

#include <kde_terminal_interface.h>

#include <tdeparts/part.h>

#include <kurl.h>
#include <klibloader.h>
#include <tdelocale.h>
#include <kdebug.h>
#include <tdemessagebox.h>

KateConsole::KateConsole (KateMainWindow *mw, KateMDI::ToolView* parent)
 : TQVBox (parent)
 , m_part (0)
 , m_mw (mw)
 , m_toolView (parent)
{
}

KateConsole::~KateConsole ()
{
  if (m_part)
    disconnect ( m_part, TQ_SIGNAL(destroyed()), this, TQ_SLOT(slotDestroyed()) );
}

void KateConsole::loadConsoleIfNeeded()
{
  if (m_part) return;

  if (!topLevelWidget() || !parentWidget()) return;
  if (!topLevelWidget() || !isVisibleTo(topLevelWidget())) return;

  KLibFactory *factory = KLibLoader::self()->factory("libkonsolepart");

  if (!factory) return;

  m_part = static_cast<KParts::ReadOnlyPart *>(factory->create(this,"libkonsolepart", "KParts::ReadOnlyPart"));

  if (!m_part) return;

  setFocusProxy(m_part->widget());

  TDEGlobal::locale()->insertCatalogue("konsole");

  m_part->widget()->show();

  connect ( m_part, TQ_SIGNAL(destroyed()), this, TQ_SLOT(slotDestroyed()) );

  if (m_mw->viewManager()->activeView())
    if (m_mw->viewManager()->activeView()->getDoc()->url().isValid())
      cd(KURL( m_mw->viewManager()->activeView()->getDoc()->url().path() ));
}

void KateConsole::slotDestroyed ()
{
  m_part = 0;

  // hide the dockwidget
  if (parentWidget())
  {
    m_mw->hideToolView (m_toolView);
    m_mw->centralWidget()->setFocus ();
  }
}

void KateConsole::showEvent(TQShowEvent *)
{
  if (m_part) return;

  loadConsoleIfNeeded();
}

void KateConsole::cd (const KURL &url)
{
  loadConsoleIfNeeded();

  if (!m_part) return;

  m_part->openURL (url);
}

void KateConsole::sendInput( const TQString& text )
{
  loadConsoleIfNeeded();

  if (!m_part) return;

  TerminalInterface *t = static_cast<TerminalInterface*>( m_part->tqt_cast( "TerminalInterface" ) );

  if (!t) return;

  t->sendInput (text);
}

void KateConsole::slotPipeToConsole ()
{
  if (KMessageBox::warningContinueCancel
      (m_mw
       , i18n ("Do you really want to pipe the text to the console? This will execute any contained commands with your user rights.")
       , i18n ("Pipe to Console?")
       , i18n("Pipe to Console"), "Pipe To Console Warning") != KMessageBox::Continue)
    return;

  Kate::View *v = m_mw->viewManager()->activeView();

  if (!v)
    return;

  if (v->getDoc()->hasSelection ())
    sendInput (v->getDoc()->selection());
  else
    sendInput (v->getDoc()->text());
}