Konsole
Part.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "Part.h"
00022
00023
00024 #include <QtCore/QStringList>
00025
00026
00027
00028 #include <KAction>
00029 #include <KActionCollection>
00030 #include <KDebug>
00031 #include <KLocale>
00032 #include <KWindowSystem>
00033 #include <kdeversion.h>
00034 #include <kde_file.h>
00035
00036
00037 #include "ColorScheme.h"
00038 #include "EditProfileDialog.h"
00039 #include "Emulation.h"
00040 #include "KeyboardTranslator.h"
00041 #include "ManageProfilesDialog.h"
00042 #include "Session.h"
00043 #include "SessionController.h"
00044 #include "SessionManager.h"
00045 #include "TerminalDisplay.h"
00046 #include "ViewManager.h"
00047 #include "MainWindow.h"
00048
00049
00050 #include "config-konsole.h"
00051 #ifdef Q_WS_X11
00052 #include <X11/Xlib.h>
00053 #ifdef HAVE_XRENDER
00054 #include <X11/extensions/Xrender.h>
00055 #endif
00056 #endif
00057
00058 extern "C"
00059 {
00060
00061
00062 KDE_EXPORT void* init_libkonsolepart()
00063 {
00064 return new Konsole::PartFactory;
00065 }
00066 }
00067
00068 using namespace Konsole;
00069
00070 KParts::Part* PartFactory::createPartObject( QWidget* parentWidget,
00071 QObject* parent,
00072 const char* ,
00073 const QStringList& )
00074 {
00075 return new Part(parentWidget,parent);
00076 }
00077
00078 K_EXPORT_PLUGIN(Konsole::PartFactory())
00079
00080 Part::Part(QWidget* parentWidget , QObject* parent)
00081 : KParts::ReadOnlyPart(parent)
00082 ,_viewManager(0)
00083 ,_pluggedController(0)
00084 ,_manageProfilesAction(0)
00085 {
00086
00087 KGlobal::locale()->insertCatalog("konsole");
00088
00089 TerminalDisplay::HAVE_TRANSPARENCY = transparencyAvailable();
00090
00091
00092 createGlobalActions();
00093
00094
00095 _viewManager = new ViewManager(this,actionCollection());
00096 _viewManager->setNavigationMethod( ViewManager::NoNavigation );
00097
00098 connect( _viewManager , SIGNAL(activeViewChanged(SessionController*)) , this ,
00099 SLOT(activeViewChanged(SessionController*)) );
00100 connect( _viewManager , SIGNAL(empty()) , this , SLOT(terminalExited()) );
00101 connect( _viewManager , SIGNAL(newViewRequest()) , this , SLOT(newTab()) );
00102
00103 _viewManager->widget()->setParent(parentWidget);
00104
00105 setWidget(_viewManager->widget());
00106 actionCollection()->addAssociatedWidget(_viewManager->widget());
00107 foreach (QAction* action, actionCollection()->actions())
00108 action->setShortcutContext(Qt::WidgetWithChildrenShortcut);
00109
00110
00111 createSession();
00112 }
00113 Part::~Part()
00114 {
00115 SessionManager::instance()->saveState();
00116 }
00117 void Part::createGlobalActions()
00118 {
00119 _manageProfilesAction = new QAction(i18n("Manage Profiles..."),this);
00120 connect(_manageProfilesAction,SIGNAL(triggered()),this,SLOT(showManageProfilesDialog()));
00121 }
00122 void Part::setupActionsForSession(SessionController* session)
00123 {
00124 KActionCollection* collection = session->actionCollection();
00125 collection->addAction("manage-profiles",_manageProfilesAction);
00126 }
00127 bool Part::transparencyAvailable()
00128 {
00129 #if defined(Q_WS_X11) && defined(HAVE_XRENDER)
00130 bool ARGB = false;
00131
00132 int screen = QX11Info::appScreen();
00133 bool depth = (QX11Info::appDepth() == 32);
00134
00135 Display* display = QX11Info::display();
00136 Visual* visual = static_cast<Visual*>(QX11Info::appVisual(screen));
00137
00138 XRenderPictFormat* format = XRenderFindVisualFormat(display, visual);
00139
00140 if (depth && format->type == PictTypeDirect && format->direct.alphaMask)
00141 {
00142 ARGB = true;
00143 }
00144
00145 if (ARGB)
00146 {
00147 return KWindowSystem::compositingActive();
00148 }
00149 else
00150 #endif
00151 {
00152 return false;
00153 }
00154 }
00155
00156 bool Part::openFile()
00157 {
00158 return false;
00159 }
00160 void Part::terminalExited()
00161 {
00162 deleteLater();
00163 }
00164 void Part::newTab()
00165 {
00166 createSession();
00167 showShellInDir( QString() );
00168 }
00169 Session* Part::activeSession() const
00170 {
00171 if ( _viewManager->activeViewController() )
00172 {
00173 Q_ASSERT( _viewManager->activeViewController()->session());
00174
00175 return _viewManager->activeViewController()->session();
00176 }
00177 else
00178 {
00179 return 0;
00180 }
00181 }
00182 void Part::startProgram( const QString& program,
00183 const QStringList& arguments )
00184 {
00185 Q_ASSERT( activeSession() );
00186
00187 if ( !activeSession()->isRunning() )
00188 {
00189 if ( !program.isEmpty() && !arguments.isEmpty() )
00190 {
00191 activeSession()->setProgram(program);
00192 activeSession()->setArguments(arguments);
00193 }
00194
00195 activeSession()->run();
00196 }
00197 }
00198 void Part::openTeletype(int fd)
00199 {
00200 Q_ASSERT( activeSession() );
00201
00202 activeSession()->openTeletype(fd);
00203 }
00204 void Part::showShellInDir( const QString& dir )
00205 {
00206 Q_ASSERT( activeSession() );
00207
00208 if ( !activeSession()->isRunning() )
00209 {
00210 if ( !dir.isEmpty() )
00211 activeSession()->setInitialWorkingDirectory(dir);
00212 activeSession()->run();
00213 }
00214 }
00215 void Part::sendInput( const QString& text )
00216 {
00217 Q_ASSERT( activeSession() );
00218 activeSession()->emulation()->sendText(text);
00219 }
00220
00221 Session* Part::createSession(const Profile::Ptr profile)
00222 {
00223 Session* session = SessionManager::instance()->createSession(profile);
00224 _viewManager->createView(session);
00225
00226 return session;
00227 }
00228 void Part::activeViewChanged(SessionController* controller)
00229 {
00230 Q_ASSERT( controller );
00231 Q_ASSERT( controller->view() );
00232
00233
00234 if (_pluggedController)
00235 {
00236 removeChildClient (_pluggedController);
00237 disconnect(_pluggedController,SIGNAL(titleChanged(ViewProperties*)),this,
00238 SLOT(activeViewTitleChanged(ViewProperties*)));
00239 }
00240
00241
00242 setupActionsForSession(controller);
00243 insertChildClient(controller);
00244 connect(controller,SIGNAL(titleChanged(ViewProperties*)),this,
00245 SLOT(activeViewTitleChanged(ViewProperties*)));
00246 activeViewTitleChanged(controller);
00247
00248 const char* displaySignal = SIGNAL(overrideShortcutCheck(QKeyEvent*,bool&));
00249 const char* partSlot = SLOT(overrideTerminalShortcut(QKeyEvent*,bool&));
00250
00251 disconnect(controller->view(),displaySignal,this,partSlot);
00252 connect(controller->view(),displaySignal,this,partSlot);
00253
00254 _pluggedController = controller;
00255 }
00256 void Part::overrideTerminalShortcut(QKeyEvent* event, bool& override)
00257 {
00258
00259 override = true;
00260 emit overrideShortcut(event,override);
00261 }
00262 void Part::activeViewTitleChanged(ViewProperties* properties)
00263 {
00264 emit setWindowCaption(properties->title());
00265 }
00266 void Part::showManageProfilesDialog()
00267 {
00268 showManageProfilesDialog(_viewManager->widget());
00269 }
00270 void Part::showManageProfilesDialog(QWidget* parent)
00271 {
00272 ManageProfilesDialog* dialog = new ManageProfilesDialog(parent);
00273 dialog->setAttribute(Qt::WA_DeleteOnClose);
00274 dialog->setShortcutEditorVisible(false);
00275 dialog->show();
00276 }
00277 void Part::showEditCurrentProfileDialog(QWidget* parent)
00278 {
00279 Q_ASSERT( activeSession() );
00280
00281 EditProfileDialog* dialog = new EditProfileDialog(parent);
00282 dialog->setAttribute(Qt::WA_DeleteOnClose);
00283 dialog->setProfile( SessionManager::instance()->sessionProfile(activeSession()) );
00284 dialog->show();
00285 }
00286 void Part::changeSessionSettings(const QString& text)
00287 {
00288
00289
00290
00291 Q_ASSERT( activeSession() );
00292 QByteArray buffer;
00293 buffer.append("\033]50;").append(text.toUtf8()).append('\a');
00294
00295 activeSession()->emulation()->receiveData(buffer.constData(),buffer.length());
00296 }
00297
00298
00299 bool Part::openUrl( const KUrl & _url )
00300 {
00301 if ( url() == _url ) {
00302 emit completed();
00303 return true;
00304 }
00305
00306 setUrl( _url );
00307 emit setWindowCaption( _url.pathOrUrl() );
00308
00309 emit started( 0 );
00310
00311 if ( _url.isLocalFile() ) {
00312 KDE_struct_stat buff;
00313 KDE_stat( QFile::encodeName( _url.path() ), &buff );
00314 QString text = ( S_ISDIR( buff.st_mode ) ? _url.path() : _url.directory() );
00315 showShellInDir( text );
00316 }
00317
00318 emit completed();
00319 return true;
00320 }
00321
00322 #include "Part.moc"