summaryrefslogtreecommitdiff
path: root/tools/cli/main.cpp
blob: caf44630e5be92b61e676d7c59f7bc80b9e33c8e (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
#include <QCoreApplication>
#include <QDBusConnection>
#include <QTimer>
#include "ApplicationManagerIface.h"

static int query(OrgKazoeDesktopApplicationsInterface &iface, int argc, char *argv[])
{
    if(argc < 2 || QString(argv[1]) == "list")
    {
        int current = iface.currentApplicationPid().value();
        printf(" ______________________________________________________\n");
        printf("| ID     | Name                             | PID      |\n");
        printf("|--------|----------------------------------|----------|\n");
        if(current == -1)
        {
            printf("|> HOME  | Homepage                         |          |\n");
        }
        else
        {
            printf("|  HOME  | Homepage                         |          |\n");
        }

        for(int i = 0; i < iface.count(); i++)
        {
            int pid = iface.applicationPid(i).value();
            if(current == pid)
            {
                printf("|> @%04i | ", i);
            }
            else
            {
                printf("|  @%04i | ", i);
            }
            printf("%-32s |", QString(iface.applicationName(i)).mid(0,32).toStdString().c_str());
            if(pid) printf("% 9i | ", iface.applicationPid(i).value());
            else printf("          |");
            printf("\n");
        }
        printf("--------------------------------------------------------\n");
        return 0;
    }

    if(QString(argv[1]) == "focus" && argc == 3)
    {
        int id = -1;
        QString ids = QString(argv[2]);
        if(ids.startsWith("@"))
        {
            id = ids.mid(1).toInt();
            return iface.applicationFocus(id).value();
        }
        for(int i = 0; i < iface.count(); i++)
        {
            QString name = iface.applicationName(i);
            if(name == ids)
            {
                return iface.applicationFocus(i).value();
            }
        }
        return -1;
    }

    if(QString(argv[1]) == "home")
    {
        iface.backHome();
        return 0;
    }

    return -1;
}

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    QDBusConnection connection = QDBusConnection::sessionBus();

    if (!connection.isConnected()) {
        qWarning("Cannot connect to the D-Bus session bus.\n"
                 "Please check your system settings and try again.\n");
        return 1;
    }
    const QString service = "org.kazoe.desktop.applications";
    const QString path = "/applications";
    OrgKazoeDesktopApplicationsInterface iface(service, path, connection);

    QTimer::singleShot(0, [argc, argv, &iface](){
        if(query(iface, argc, argv) != 0)
        {
            qInfo() << "Usage:";
            qInfo() << "> desktopcli list              -> List all views";
            qInfo() << "> desktopcli home              -> Go to home view";
            qInfo() << "> desktopcli focus <ID | Name> -> Go to application view, by ID or Name";
        }
        exit(0);
    });

    return app.exec();
}