source: calamares/trunk/fuentes/src/modules/interactiveterminal/InteractiveTerminalPage.cpp @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 13 months ago

sync with github

File size: 3.7 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
4 *
5 *   Calamares is free software: you can redistribute it and/or modify
6 *   it under the terms of the GNU General Public License as published by
7 *   the Free Software Foundation, either version 3 of the License, or
8 *   (at your option) any later version.
9 *
10 *   Calamares is distributed in the hope that it will be useful,
11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 *   GNU General Public License for more details.
14 *
15 *   You should have received a copy of the GNU General Public License
16 *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "InteractiveTerminalPage.h"
20
21#include "viewpages/ViewStep.h"
22#include "utils/Retranslator.h"
23#include "utils/CalamaresUtilsGui.h"
24#include "utils/Logger.h"
25
26#include <KF5/KService/kservice.h>
27#include <KF5/KParts/kde_terminal_interface.h>
28#include <KF5/KParts/kparts/readonlypart.h>
29
30#include <QApplication>
31#include <QDir>
32#include <QLabel>
33#include <QMessageBox>
34#include <QVBoxLayout>
35
36
37InteractiveTerminalPage::InteractiveTerminalPage( QWidget* parent )
38    : QWidget( parent )
39    , m_layout( new QVBoxLayout( this ) )
40    , m_termHostWidget( nullptr )
41{
42    setLayout( m_layout );
43    m_layout->setContentsMargins( 0, 0, 0, 0 );
44
45    m_headerLabel = new QLabel( this );
46    m_layout->addWidget( m_headerLabel );
47}
48
49void
50InteractiveTerminalPage::errorKonsoleNotInstalled()
51{
52    QMessageBox::critical( this,
53                           tr( "Konsole not installed"),
54                           tr( "Please install KDE Konsole and try again!" ),
55                           QMessageBox::Ok );
56}
57
58void
59InteractiveTerminalPage::onActivate()
60{
61    if ( m_termHostWidget )
62        return;
63    // For whatever reason, instead of simply linking against a library we
64    // need to do a runtime query to KService just to get a sodding terminal
65    // widget.
66    KService::Ptr service = KService::serviceByDesktopName( "konsolepart" );
67    if ( !service )
68    {
69        // And all of this hoping the Konsole application is installed. If not,
70        // tough cookies.
71        errorKonsoleNotInstalled();
72        return ;
73    }
74
75    // Create one instance of konsolepart.
76    KParts::ReadOnlyPart* p =
77        service->createInstance< KParts::ReadOnlyPart >( this,
78                                                         this,
79                                                         {} );
80    if ( !p )
81    {
82        // One more opportunity for the loading operation to fail.
83        errorKonsoleNotInstalled();
84        return;
85    }
86
87    // Cast the konsolepart to the TerminalInterface...
88    TerminalInterface* t = qobject_cast< TerminalInterface* >( p );
89    if ( !t )
90    {
91        // This is why we can't have nice things.
92        errorKonsoleNotInstalled();
93        return;
94    }
95
96    // Make the widget persist even if the KPart goes out of scope...
97    p->setAutoDeleteWidget( false );
98    // ... but kill the KPart if the widget goes out of scope.
99    p->setAutoDeletePart( true );
100
101    m_termHostWidget = p->widget();
102    m_layout->addWidget( m_termHostWidget );
103    cDebug() << "Part widget ought to be"
104             << m_termHostWidget->metaObject()->className();
105
106    t->showShellInDir( QDir::home().path() );
107    t->sendInput( QString( "%1\n" ).arg( m_command ) );
108}
109
110
111void
112InteractiveTerminalPage::setCommand( const QString& command )
113{
114    m_command = command;
115    CALAMARES_RETRANSLATE(
116        m_headerLabel->setText( tr( "Executing script: &nbsp;<code>%1</code>" )
117                                .arg( m_command ) );
118    )
119}
Note: See TracBrowser for help on using the repository browser.