File: xdgcachedmenu.cpp

package info (click to toggle)
lxqt-panel 2.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 15,748 kB
  • sloc: cpp: 27,548; xml: 798; makefile: 19
file content (216 lines) | stat: -rw-r--r-- 6,968 bytes parent folder | download
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
 * <one line to give the library's name and an idea of what it does.>
 * Copyright (C) 2013  <copyright holder> <email>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "xdgcachedmenu.h"
#include <QAction>
#include <QIcon>
#include <QCursor>
#include <QToolTip>
#include <QList>
#include <QUrl>
#include <QDrag>
#include <QMouseEvent>
#include <QApplication>
#include <XdgDesktopFile>
#include <QHelpEvent>
#include <QMimeData>
#include <QDebug>
#include <memory>

XdgCachedMenuAction::XdgCachedMenuAction(MenuCacheItem* item, QObject* parent):
    QAction{parent}
    , iconName_{QString::fromUtf8(menu_cache_item_get_icon(item))}
{
    QString title = QString::fromUtf8(menu_cache_item_get_name(item));
    title = title.replace(QLatin1Char('&'), QLatin1String("&&")); // & is reserved for mnemonics
    setText(title);
    // Only set tooltips for app items
    if(menu_cache_item_get_type(item) == MENU_CACHE_TYPE_APP)
    {
        QString comment = QString::fromUtf8(menu_cache_item_get_comment(item));
        setToolTip(comment);
    }
    if (char * file_path = menu_cache_item_get_file_path(item))
    {
        filePath_ = QString::fromUtf8(file_path);
        g_free(file_path);
    }
}

void XdgCachedMenuAction::updateIcon()
{
    if(icon().isNull())
    {
        // Note: We don't use the QIcon::fromTheme(const QString &name
        // , const QIcon &fallback) overload because of "availableSizes()"
        // check in it, see https://bugreports.qt.io/browse/QTBUG-63187
        QIcon icon = QIcon::fromTheme(iconName_);

        if (icon.isNull())
            icon = QIcon::fromTheme(QStringLiteral("unknown"));
        // Some themes may lack the "unknown" icon; checking null prevents 
        // infinite recursion (setIcon->dataChanged->updateIcon->setIcon)
        if (icon.isNull())
            return;
        setIcon(icon);
    }
}

XdgCachedMenu::XdgCachedMenu(QWidget* parent): QMenu(parent)
{
    connect(this, &QMenu::aboutToShow, this, &XdgCachedMenu::onAboutToShow);
}

XdgCachedMenu::XdgCachedMenu(MenuCache* menuCache, QWidget* parent): QMenu(parent)
{
    // qDebug() << "CREATE MENU FROM CACHE" << menuCache;
    MenuCacheDir* dir = menu_cache_dup_root_dir(menuCache);

    // get current desktop name or fallback to LXQt
    const QByteArray xdgDesktop = qgetenv("XDG_CURRENT_DESKTOP");
    const QByteArray desktop = xdgDesktop.isEmpty() ? "LXQt:X-LXQt" : xdgDesktop;
    menu_cache_desktop_ = menu_cache_get_desktop_env_flag(menuCache, desktop.constData());

    addMenuItems(this, dir);
    menu_cache_item_unref(MENU_CACHE_ITEM(dir));
    connect(this, &QMenu::aboutToShow, this, &XdgCachedMenu::onAboutToShow);
}

XdgCachedMenu::~XdgCachedMenu()
{
}

void XdgCachedMenu::addMenuItems(QMenu* menu, MenuCacheDir* dir)
{
  GSList* list = menu_cache_dir_list_children(dir);
  for(GSList * l = list; l; l = l->next)
  {
    // Note: C++14 is needed for usage of the std::make_unique
    //auto guard = std::make_unique(static_cast<MenuCacheItem *>(l->data), menu_cache_item_unref);
    std::unique_ptr<MenuCacheItem, gboolean (*)(MenuCacheItem* item)> guard{static_cast<MenuCacheItem *>(l->data), menu_cache_item_unref};
    MenuCacheItem* item = guard.get();
    MenuCacheType type = menu_cache_item_get_type(item);

    if(type == MENU_CACHE_TYPE_SEP)
    {
      menu->addSeparator();
      continue;
    }
    else
    {
      bool appVisible = type == MENU_CACHE_TYPE_APP
          && menu_cache_app_get_is_visible(MENU_CACHE_APP(item),
                                           menu_cache_desktop_);
      bool dirVisible = type == MENU_CACHE_TYPE_DIR
          && menu_cache_dir_is_visible(MENU_CACHE_DIR(item));

      if(!appVisible && !dirVisible)
        continue;

      XdgCachedMenuAction* action = new XdgCachedMenuAction(item, menu);
      menu->addAction(action);

      if(type == MENU_CACHE_TYPE_APP)
        connect(action, &QAction::triggered, this, &XdgCachedMenu::onItemTriggered);
      else if(type == MENU_CACHE_TYPE_DIR)
      {
        XdgCachedMenu* submenu = new XdgCachedMenu(menu);
        action->setMenu(submenu);
        addMenuItems(submenu, MENU_CACHE_DIR(item));
      }
    }
  }
  if (list)
      g_slist_free(list);
}

void XdgCachedMenu::onItemTriggered()
{
    XdgCachedMenuAction* action = static_cast<XdgCachedMenuAction*>(sender());
    XdgDesktopFile df;
    df.load(action->filePath());
    df.startDetached();
}

// taken from libqtxdg: XdgMenuWidget
bool XdgCachedMenu::event(QEvent* event)
{
    if (event->type() == QEvent::MouseButtonPress)
    {
        QMouseEvent *e = static_cast<QMouseEvent*>(event);
        if (e->button() == Qt::LeftButton)
            mDragStartPosition = e->pos();
    }

    else if (event->type() == QEvent::MouseMove)
    {
        QMouseEvent *e = static_cast<QMouseEvent*>(event);
        handleMouseMoveEvent(e);
    }

    else if(event->type() == QEvent::ToolTip)
    {
        QHelpEvent* helpEvent = static_cast<QHelpEvent*>(event);
        QAction* action = actionAt(helpEvent->pos());
        if(action && action->menu() == nullptr)
            QToolTip::showText(helpEvent->globalPos(), action->toolTip(), this);
    }

    return QMenu::event(event);
}

// taken from libqtxdg: XdgMenuWidget
void XdgCachedMenu::handleMouseMoveEvent(QMouseEvent *event)
{
    if (!(event->buttons() & Qt::LeftButton))
        return;

    if ((event->pos() - mDragStartPosition).manhattanLength() < QApplication::startDragDistance())
        return;

    XdgCachedMenuAction *a = qobject_cast<XdgCachedMenuAction*>(actionAt(mDragStartPosition));
    if (!a)
        return;

    QList<QUrl> urls;
    urls << QUrl(QString::fromLatin1("file://%1").arg(a->filePath()));

    QMimeData *mimeData = new QMimeData();
    mimeData->setUrls(urls);

    QDrag *drag = new QDrag(this);
    drag->setMimeData(mimeData);
    drag->exec(Qt::CopyAction | Qt::LinkAction);
}

void XdgCachedMenu::onAboutToShow()
{
    const auto actionList = actions();
    for(QAction* action : actionList)
    {
        if(action->inherits("XdgCachedMenuAction"))
        {
            static_cast<XdgCachedMenuAction*>(action)->updateIcon();

            // this seems to cause some incorrect menu behaviour.
            // qApp->processEvents();
        }
    }
}