void PidginRunner::match(Plasma::RunnerContext &context)
{
    qDebug() << Q_FUNC_INFO ;
    QString query = context.query();
    if ( !query.isEmpty() )
    {
        qDebug() << " Pidgin runner trigger word";
        QString contactName = query ;

        // so now let's filter all contacts from
        // pidgin
        QStringList contacts = pidgin_d.search(query);
        QList<Plasma::QueryMatch> matches;
        std::for_each(contacts.begin(), contacts.end(), [&contacts, &matches, this](const QString& c )
        {
            Plasma::QueryMatch match(this);
            QVariantMap map = pidgin_d.buddyId(c);
            match.setText(c);
            match.setSubtext(map["buddyStatus"].toString());
            match.setData(c);
            match.setId(c);
            match.setType(Plasma::QueryMatch::ExactMatch);
            QIcon icon(map["buddyIconPath"].toString());
            match.setIcon(icon);
            matches.append(match);

        });
        context.addMatches(matches);
    }
}
Exemple #2
0
void PlasmaRunner::match(Plasma::RunnerContext &context)
{
    QList<Plasma::QueryMatch> matches;

    const QString query = context.query();

    bool success = false;
    // TODO: how to estimate that input is in Degree, not Radian?
    GeoDataCoordinates coordinates = GeoDataCoordinates::fromString(query, success);

    if (success) {
        const QVariant coordinatesData = QVariantList()
            << QVariant(coordinates.longitude(GeoDataCoordinates::Degree))
            << QVariant(coordinates.latitude(GeoDataCoordinates::Degree))
            << QVariant(0.1); // TODO: make this distance value configurable

        Plasma::QueryMatch match(this);
        match.setIcon(QIcon::fromTheme(QStringLiteral("marble")));
        match.setText(i18n("Show the coordinates %1 in OpenStreetMap with Marble", query));
        match.setData(coordinatesData);
        match.setId(query);
        match.setRelevance(1.0);
        match.setType(Plasma::QueryMatch::ExactMatch);

        matches << match;
    }

    // TODO: BookmarkManager does not yet listen to updates, also does not sync between processes :(
    // So for now always load on demand, even if expensive possibly
    BookmarkManager bookmarkManager(new GeoDataTreeModel);
    bookmarkManager.loadFile( QStringLiteral("bookmarks/bookmarks.kml") );

    for (GeoDataFolder* folder: bookmarkManager.folders()) {
        collectMatches(matches, query, folder);
    }

    if ( ! matches.isEmpty() ) {
        context.addMatches(matches);
    }
}
void KopeteRunner::match(Plasma::RunnerContext& context)
{
    if (!m_loaded) {
        return;
    }

    const QString term = context.query().toLower();
    if (term.length() < 3) {
        return;
    }

    QList<QueryMatch> matches;

    if (term == "connect")
    {
        QueryMatch match(this);

        match.setType(QueryMatch::ExactMatch);
        match.setIcon(KIcon("user-"));
        match.setText(i18n("Set all accounts as online"));
        match.setData("connect");

        matches.append(match);
    }
    else if (term == "disconnect")
    {
        QueryMatch match(this);

        match.setType(QueryMatch::ExactMatch);
        match.setIcon(KIcon("user-offline"));
        match.setText(i18n("Set all accounts as offline"));
        match.setData("disconnect");

        matches.append(match);
    }
    else if (term.startsWith(QLatin1String("status")))
    {
        QStringList query = context.query().split(' ');
        // Take the status text
        query.takeFirst();
        if (!query.isEmpty())
        {
            // Take the status to set
            const QString status = query.takeFirst();
            if (!status.isEmpty())
            {
                QueryMatch match(this);

                match.setType(QueryMatch::ExactMatch);
                match.setIcon(KIcon("user-away"));
                match.setText(i18nc("The \': \' is used as a separator", "Status: %1", status));
                // Rejoin the status message
                const QString message = query.join(" ");
                if (!message.isEmpty())
                    match.setSubtext(i18nc("The \': \' is used as a separator", "Message: %1", message));
                match.setData("status");

                matches.append(match);
            }
        }
    }
    else if (term.startsWith(QLatin1String("message")))
    {
        QStringList query = context.query().split(' ');
        // Take the status text
        query.takeFirst();
        if (!query.isEmpty())
        {
            // Rejoin the rest of the message
            const QString message = query.join(" ");
            if (!message.isEmpty())
            {
                QueryMatch match(this);

                match.setType(QueryMatch::ExactMatch);
                match.setIcon(KIcon("im-status-message-edit"));
                match.setText(i18nc("The \': \' is used as a separator", "Message: %1", message));
                match.setData(i18n("Set Status Message"));
                match.setData("status");

                matches.append(match);
            }
        }
    }
    QHashIterator<QString, QVariantMap> i(m_contactData);
    while (i.hasNext()) {
	i.next();
        // Keep a reference for easier use
        const ContactProperties& props = i.value();
        // Skip unreachable contacts
        if (!props["message_reachable"].toBool())
            continue;

        const QString name = props["display_name"].toString();
        const QString picture = props["picture"].toString();
        const QString status = props["status"].toString();
        const QString message = props["status_message"].toString();
        if (name.contains(term, Qt::CaseInsensitive))
        {
            QueryMatch match(this);

            match.setType((name.compare(context.query(), Qt::CaseInsensitive)) ? QueryMatch::PossibleMatch : QueryMatch::ExactMatch);
            match.setIcon(KIcon(KUrl(picture).isLocalFile() ? picture : "kopete"));
            match.setText(i18n("Send message to %1", name));
            const QString statusLine = i18n("Status: %1", status);
            const QString subtext = message.isEmpty() ? statusLine : i18n("%1\nMessage: %2", statusLine, message);
            match.setSubtext(subtext);
            match.setData(i.key());

            matches.append(match);
        }
    }

    context.addMatches(term, matches);
}
void AudioPlayerControlRunner::match(Plasma::RunnerContext &context)
{
    if (context.query().length() < 3) {
        return;
    }

    const QString term = context.query();

    QList<Plasma::QueryMatch> matches;

    if (m_useCommands) {
        /* DBus paths that are used in the command executes */
        /* The data variable looks like this:
         * "/PlayerQLatin1String( " " )org.freedesktop.MediaPlayerQLatin1String( " " )PlayQLatin1String( " " )actionsQLatin1String( " " )start" args...
         * <path>    <interface>                 <method> <actions> <start player>
         * <actions> is NONE if no action is needed
         */

        QVariantList playcontrol;
        playcontrol  << QLatin1String( "/Player" ) << QLatin1String( "org.freedesktop.MediaPlayer" );

        /* The commands */

        //Play
        if (context.isValid() && m_comPlay.startsWith(term, Qt::CaseInsensitive) &&
	    (!m_running || m_songsInPlaylist)) {
            QVariantList data = playcontrol;
            data << ((currentSong() == -1) ? QLatin1String( "Next" ) : QLatin1String( "Play" )) << NONE << QLatin1String( "start" );
            matches << createMatch(this, i18n("Start playing"), i18n("Audio player control"), QLatin1String( "play" ),
                                   KIcon( QLatin1String( "media-playback-start" )), data, 1.0);
        }

        if (!context.isValid() || !m_running) {
            //The interface of the player is not availalbe, so the rest of the commands
            //is not needed
            context.addMatches(matches);
            return;
        }

        if (context.isValid() && m_songsInPlaylist) {
            //The playlist isn't empty
            //Next song
            if (m_comNext.startsWith(term,Qt::CaseInsensitive)
                    && m_nextSongAvailable) {
                QVariantList data = playcontrol;
                data << QLatin1String( "Next" ) << NONE << QLatin1String( "nostart" );
                matches << createMatch(this, i18n("Play next song"), i18n("Audio player control"),
                                       QLatin1String( "next" ), KIcon( QLatin1String( "media-skip-forward" )), data, 1.0);
            }

            //Previous song
            if (context.isValid() && m_comPrev.startsWith(term,Qt::CaseInsensitive)
                    && m_prevSongAvailable) {
                QVariantList data = playcontrol;
                data << QLatin1String( "Prev" ) << NONE << QLatin1String( "nostart" );
                matches << createMatch(this, i18n("Play previous song"), i18n("Audio player control") ,
                                       QLatin1String( "previous" ), KIcon( QLatin1String( "media-skip-backward" )), data, 1.0);
            }
        }//--- if (m_songsInPlaylist)

        //Pause
        if (context.isValid() && m_comPause.startsWith(term,Qt::CaseInsensitive)) {
            QVariantList data = playcontrol;
            data << QLatin1String( "Pause" ) << NONE << QLatin1String( "nostart" );
            matches << createMatch(this, i18n("Pause playing"), i18n("Audio player control"),
                                   QLatin1String( "pause" ), KIcon( QLatin1String( "media-playback-pause" )), data, 1.0);
        }

        //Stop
        if (context.isValid() && m_comStop.startsWith(term,Qt::CaseInsensitive)) {
            QVariantList data = playcontrol;
            data << QLatin1String( "Stop" ) << NONE << QLatin1String( "nostart" );
            matches << createMatch(this, i18n("Stop playing"), i18n("Audio player control"),
                                   QLatin1String( "stop" ), KIcon( QLatin1String( "media-playback-stop" )), data, 1.0);
        }

        //Increase
        if (context.isValid() && m_comIncrease.startsWith(term,Qt::CaseInsensitive)) {
            QVariantList data = playcontrol;
            data << QLatin1String( "VolumeUp" ) << NONE << QLatin1String( "nostart" ) << m_increaseBy;
            matches << createMatch(this, i18n("Increase volume by %1" , m_increaseBy),
                                   QLatin1String( "volumeup" ), i18n("Audio player control"), KIcon(QLatin1String( "audio-volume-high" )), data, 1.0);
        } else if (context.isValid() && equals(term, QRegExp( m_comIncrease + QLatin1String( " \\d{1,2}0{0,1}" ) ) ) ) {
            int volumeChange = getNumber(term, ' ' );
            QVariantList data = playcontrol;
            data << QLatin1String( "VolumeUp" ) << NONE << QLatin1String( "nostart" ) << volumeChange;
            matches << createMatch(this, i18n("Increase volume by %1" , volumeChange),
                                   QLatin1String( "volumeup" ), i18n("Audio player control"), KIcon(QLatin1String( "audio-volume-high" )), data, 1.0);
        }

        //Decrease
        if (context.isValid() && m_comDecrease.startsWith(term,Qt::CaseInsensitive)) {
            QVariantList data = playcontrol;
            data << QLatin1String( "VolumeDown" ) << NONE << QLatin1String( "nostart" ) << m_decreaseBy;
            matches << createMatch(this, i18n("Reduce volume by %1", m_decreaseBy),
                                   QLatin1String( "volumedown" ), i18n("Audio player control"), KIcon(QLatin1String( "audio-volume-low" )), data, 1.0);
        } else if (context.isValid() && equals(term, QRegExp( m_comDecrease + QLatin1String( " \\d{1,2}0{0,1}" ) ) ) ) {
            int volumeChange = getNumber(term, ' ');
            QVariantList data = playcontrol;
            data << QLatin1String( "VolumeDown" ) << NONE << QLatin1String( "nostart" ) << volumeChange;
            matches << createMatch(this, i18n("Reduce volume by %1", volumeChange),
                                   QLatin1String( "volumedown" ), i18n("Audio player control"), KIcon(QLatin1String( "audio-volume-low" )), data, 1.0);
        }

        //Set volume to
        if (context.isValid() && equals(term, QRegExp( m_comVolume + QLatin1String( " \\d{1,2}0{0,1}" ) ) ) ) {
            QVariantList data = playcontrol;
            int newVolume = getNumber(term , ' ');
            data << QLatin1String( "VolumeSet" ) << NONE << QLatin1String( "nostart" ) << newVolume;
            matches << createMatch(this, i18n("Set volume to %1%" , newVolume),
                                   QLatin1String( "volume" ), i18n("Audio player control"), KIcon(QLatin1String( "audio-volume-medium" )), data, 1.0);
        }

        //Mute
        if (context.isValid() && m_comMute.startsWith(term,Qt::CaseInsensitive)) {
            QVariantList data = playcontrol;
            data << QLatin1String( "Mute" ) << NONE << QLatin1String( "nostart" );
            matches << createMatch(this, i18n("Mute"), i18n("Audio player control"),
                                   QLatin1String( "mute" ), KIcon( QLatin1String( "audio-volume-muted" )), data, 1.0);
        }

        //Quit player
        if (context.isValid() && m_comQuit.startsWith(term,Qt::CaseInsensitive)) {
            QVariantList data;
            data  << QLatin1String( "/" ) << QLatin1String( "org.freedesktop.MediaPlayer" ) << QLatin1String( "Quit" ) << NONE
            << QLatin1String( "nostart" );
            matches << createMatch(this, i18n("Quit %1", m_player),QLatin1String( "" ),
                                   QLatin1String( "quit" ), KIcon( QLatin1String( "application-exit" )), data, 1.0);
        }
    }//--- if (m_useCommands)

    if (context.isValid() && m_searchCollection) {
        QString actionNames;
        QString searchTerm = term;
        QString command;

        if (term.startsWith(m_comPlay,Qt::CaseInsensitive)
                && term.length() > m_comPlay.length()) {
            command = m_comPlay;
            actionNames = PLAY;
        } else if (term.startsWith(m_comAppend, Qt::CaseInsensitive)
                   && term.length() > m_comAppend.length()) {
            command = m_comAppend;
            actionNames = APPEND;
        } else if (term.startsWith(m_comQueue, Qt::CaseInsensitive)
                   && term.length() > m_comQueue.length()) {
            command = m_comQueue;
            actionNames = QUEUE;
        } else {
            actionNames = QString::fromLatin1( "%1,%2,%3").arg(PLAY).arg(APPEND).arg(QUEUE);
        }

	if (!context.isValid())
	{
	    return;
	}
        searchTerm = searchTerm.right(searchTerm.length() - (command.length() + 1));
        matches << searchCollectionFor(searchTerm, actionNames);
        //Adds matches for all song matches for term
    }

    context.addMatches(matches);
}
void PowerDevilRunner::match(Plasma::RunnerContext &context)
{
    const QString term = context.query();
    if (term.length() < m_shortestCommand) {
        return;
    }

    QList<Plasma::QueryMatch> matches;

    QString parameter;

    if (parseQuery(term,
                   QList<QRegExp>() << QRegExp(i18nc("Note this is a KRunner keyword; %1 is a parameter", "power profile %1", "(.*)"), Qt::CaseInsensitive)
                                    << QRegExp(i18nc("Note this is a KRunner keyword", "power profile"), Qt::CaseInsensitive),
                   parameter)) {
        for (StringStringMap::const_iterator i = m_availableProfiles.constBegin(); i != m_availableProfiles.constEnd(); ++i) {
            if (!parameter.isEmpty()) {
                if (!i.value().startsWith(parameter, Qt::CaseInsensitive)) {
                    continue;
                }
            }
            Plasma::QueryMatch match(this);
            match.setType(Plasma::QueryMatch::ExactMatch);
            match.setIcon(KIcon(m_profileIcon[i.key()]));
            match.setText(i18n("Set Profile to '%1'", i.value()));
            match.setData(i.key());
            match.setRelevance(1);
            match.setId("ProfileChange "+ i.key());
            matches.append(match);
        }
    } else if (parseQuery(term,
                          QList<QRegExp>() << QRegExp(i18nc("Note this is a KRunner keyword; %1 is a parameter", "screen brightness %1", "(.*)"), Qt::CaseInsensitive)
                                           << QRegExp(i18nc("Note this is a KRunner keyword", "screen brightness"), Qt::CaseInsensitive)
                                           << QRegExp(i18nc("Note this is a KRunner keyword; %1 is a parameter", "dim screen %1", "(.*)"), Qt::CaseInsensitive)
                                           << QRegExp(i18nc("Note this is a KRunner keyword", "dim screen"), Qt::CaseInsensitive),
                          parameter)) {
        if (!parameter.isEmpty()) {
            bool test;
            int b = parameter.toInt(&test);
            if (test) {
                int brightness = qBound(0, b, 100);
                Plasma::QueryMatch match(this);
                match.setType(Plasma::QueryMatch::ExactMatch);
                match.setIcon(KIcon("preferences-system-power-management"));
                match.setText(i18n("Set Brightness to %1", brightness));
                match.setData(brightness);
                match.setRelevance(1);
                match.setId("BrightnessChange");
                matches.append(match);
            }
        } else {
            Plasma::QueryMatch match1(this);
            match1.setType(Plasma::QueryMatch::ExactMatch);
            match1.setIcon(KIcon("preferences-system-power-management"));
            match1.setText(i18n("Dim screen totally"));
            match1.setRelevance(1);
            match1.setId("DimTotal");
            matches.append(match1);

            Plasma::QueryMatch match2(this);
            match2.setType(Plasma::QueryMatch::ExactMatch);
            match2.setIcon(KIcon("preferences-system-power-management"));
            match2.setText(i18n("Dim screen by half"));
            match2.setRelevance(1);
            match2.setId("DimHalf");
            matches.append(match2);

            Plasma::QueryMatch match3(this);
            match3.setType(Plasma::QueryMatch::ExactMatch);
            match3.setIcon(KIcon("video-display"));
            match3.setText(i18n("Turn off screen"));
            match3.setRelevance(1);
            match3.setId("TurnOffScreen");
            matches.append(match3);
        }
    } else if (term.compare(i18nc("Note this is a KRunner keyword", "suspend"), Qt::CaseInsensitive) == 0) {
        QSet< Solid::PowerManagement::SleepState > states = Solid::PowerManagement::supportedSleepStates();

        if (states.contains(Solid::PowerManagement::SuspendState)) {
            addSuspendMatch(Solid::PowerManagement::SuspendState, matches);
        }

        if (states.contains(Solid::PowerManagement::HibernateState)) {
            addSuspendMatch(Solid::PowerManagement::HibernateState, matches);
        }
    } else if (term.compare(i18nc("Note this is a KRunner keyword", "sleep"), Qt::CaseInsensitive) == 0 ||
               term.compare(i18nc("Note this is a KRunner keyword", "to ram"), Qt::CaseInsensitive) == 0) {
        addSuspendMatch(Solid::PowerManagement::SuspendState, matches);
    } else if (term.compare(i18nc("Note this is a KRunner keyword", "hibernate"), Qt::CaseInsensitive) == 0 ||
               term.compare(i18nc("Note this is a KRunner keyword", "to disk"), Qt::CaseInsensitive) == 0) {
        addSuspendMatch(Solid::PowerManagement::HibernateState, matches);
    }

    if (!matches.isEmpty()) {
        context.addMatches(term, matches);
    }
}