File indexing completed on 2024-04-14 03:40:40

0001 /*
0002     This file is part of Kiten, a KDE Japanese Reference Tool
0003     SPDX-FileCopyrightText: 2006 Joseph Kerian <jkerian@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "radselect.h"
0009 
0010 #include "radselectconfig.h"
0011 #include "radselectview.h"
0012 
0013 #include "ui_radselectprefdialog.h"
0014 
0015 #include <KActionCollection>
0016 #include <KConfigDialog>
0017 #include <KStandardAction>
0018 #include <KStandardShortcut>
0019 #include <KXMLGUIFactory>
0020 
0021 #include <QDBusConnection>
0022 #include <QDBusInterface>
0023 #include <QDBusMessage>
0024 #include <QDropEvent>
0025 #include <QMimeData>
0026 #include <QStatusBar>
0027 
0028 RadSelect::RadSelect()
0029     : KXmlGuiWindow()
0030     , m_view(new RadSelectView(this))
0031 {
0032     // accept dnd
0033     setAcceptDrops(true);
0034     setCentralWidget(m_view); // This is the main widget
0035     setObjectName(QStringLiteral("radselect"));
0036 
0037     KStandardAction::quit(this, SLOT(close()), actionCollection());
0038     KStandardAction::preferences(this, SLOT(optionsPreferences()), actionCollection());
0039 
0040     KStandardAction::keyBindings(guiFactory(), &KXMLGUIFactory::showConfigureShortcutsDialog, actionCollection());
0041 
0042     statusBar()->show();
0043 
0044     // Apply the create the main window and ask the mainwindow to
0045     // automatically save settings if changed: window size, toolbar
0046     // position, icon size, etc.  Also to add actions for the statusbar
0047     // toolbar, and keybindings if necessary.
0048     setupGUI(Keys | StatusBar | Save | Create, QStringLiteral("radselectui.rc"));
0049 
0050     // allow the view to change the statusbar
0051     connect(m_view, &RadSelectView::signalChangeStatusbar, this, &RadSelect::changeStatusbar);
0052 
0053     if (!QDBusConnection::sessionBus().isConnected()) {
0054         qDebug() << "Session Bus not found!!";
0055         m_dbusInterface = nullptr;
0056     } else {
0057         m_dbusInterface = new QDBusInterface(QStringLiteral("org.kde.kiten"), QStringLiteral("/"), QLatin1String(""), QDBusConnection::sessionBus());
0058     }
0059 
0060     // connect the search signal from the m_view with our dbus routines
0061     connect(m_view, &RadSelectView::kanjiSelected, this, &RadSelect::sendSearch);
0062 }
0063 
0064 RadSelect::~RadSelect()
0065 {
0066     delete m_dbusInterface;
0067 }
0068 
0069 void RadSelect::changeStatusbar(const QString &text)
0070 {
0071     // display the text on the statusbar
0072     statusBar()->showMessage(text);
0073 }
0074 
0075 void RadSelect::dragEnterEvent(QDragEnterEvent *event)
0076 {
0077     if (event->mimeData()->hasFormat(QStringLiteral("text/plain"))) {
0078         event->acceptProposedAction();
0079     }
0080 }
0081 
0082 void RadSelect::dropEvent(QDropEvent *event)
0083 {
0084     QByteArray qba = event->mimeData()->data(QStringLiteral("text/plain"));
0085     if (qba.size() > 0) {
0086         loadSearchString(QString::fromUtf8(qba));
0087     }
0088 }
0089 
0090 void RadSelect::loadSearchString(const QString &searchString)
0091 {
0092     m_currentQuery = searchString;
0093     changeStatusbar(searchString);
0094     // TODO: Parse the strokes
0095     QString strokeStr = m_currentQuery.getProperty(QStringLiteral("S"));
0096     int min = 0;
0097     int max = 0;
0098     m_view->loadRadicals(m_currentQuery.getProperty(QStringLiteral("R")), min, max);
0099 }
0100 
0101 void RadSelect::optionsPreferences()
0102 {
0103     if (KConfigDialog::showDialog(QStringLiteral("settings"))) {
0104         return;
0105     }
0106 
0107     KConfigDialog *dialog = new KConfigDialog(this, QStringLiteral("settings"), RadSelectConfigSkeleton::self());
0108     dialog->setFaceType(KPageDialog::FaceType::Plain);
0109     auto preferences = new QWidget();
0110     Ui::radselectprefdialog layout;
0111     layout.setupUi(preferences);
0112     dialog->addPage(preferences, QString(), QStringLiteral("help-contents"));
0113     connect(dialog, SIGNAL(settingsChanged(QString)), m_view, SLOT(loadSettings()));
0114     dialog->show();
0115 }
0116 
0117 void RadSelect::readProperties(const KConfigGroup &config)
0118 {
0119     // For resume
0120     loadSearchString(config.readPathEntry("searchString", QString()));
0121 }
0122 
0123 void RadSelect::saveProperties(KConfigGroup &config)
0124 {
0125     // For suspend
0126     if (!m_currentQuery.isEmpty()) {
0127         config.writePathEntry("searchString", m_currentQuery);
0128     }
0129 }
0130 
0131 // This one is triggered if the search button is used (or the widget interface
0132 // is in some other way given priority.
0133 void RadSelect::sendSearch(const QStringList &kanji)
0134 {
0135     if (kanji.empty()) {
0136         return;
0137     }
0138 
0139     // This may need to be done differently for handling collisions
0140     m_currentQuery = kanji.at(0);
0141 
0142     changeStatusbar(m_currentQuery);
0143 
0144     if (m_dbusInterface && m_dbusInterface->isValid()) {
0145         QDBusMessage reply = m_dbusInterface->call(QStringLiteral("searchTextAndRaise"), m_currentQuery.toString());
0146         if (reply.type() == QDBusMessage::ErrorMessage) {
0147             qDebug() << "QDBus Error: " << reply.signature() << "<eoe>";
0148         }
0149     }
0150 }
0151 
0152 #include "moc_radselect.cpp"