On Wednesday 18 February 2009, Aaron J. Seigo wrote:
> On Monday 16 February 2009, Marco Martin wrote:
> > On Monday 16 February 2009, Aaron J. Seigo wrote:
> > > On Sunday 15 February 2009, Marco Martin wrote:
> > > > this basic thing works fairly well by now, what is totally missing of
> > > > course is the big part, the comunication infrastructure with the app
> > > > (don't
> > >
> > > how to do this is going to be the big trick in all of this. the d-bus
> > > interaction is actually pretty simple, but preserving backwards compat
> > > will be interesting to accomplish.
> >
> > yes, and fall back at the proper cases, that is the most worrying part,
> > yeah
>
> fortunately, that's probably the easy part: test for existence of the dbus
> service :)
>
> > > we have KSystemTrayIcon which IsA QSystemTrayIcon. so even if we
> > > implement the D-Bus protocol in KSystemTrayIcon, we'll always have a
> > > QSystemTrayIcon, and therefore a regular system tray icon, as well.
> >
> > ok, so we don't need a subclass of ksystemtrayicon, but something with
> > roughly the same api, i'm going a bit blindly there because i still never
> > looked at both KSystemTrayIcon and QSystemTrayIcon
>
> if you want to focus on the plasma side, i can take a spin at the client
> side API.
that would be killer :) at least beginnings of a working library, i was 
attempting to sketch something starting from exactly the same api of 
ksystemtrayicon (kinda fail for now), not too convinced but a quasi dropin 
replacement would make hopes of being adopted a bit higher, i'm not sure its 
api can bear all the info we need, at least a sensible subset on what we can 
expand
and other things not sure it's sane to support them at all, setMovie? ouch..


> > > KNotificationIcon? meh.
> >
> > hmm, as a name wouldn't confuse  a bit with dbus notification and galago
> > things?
>
> probably.  ergo the "meh". i'm still trying to think of a non-bad name.
>
> > TrayBus (the fancy rather meaningless one)
> > NotificationIcon (the really (too much?) generic one)
>
> hm.. it should say what it's doing, but be unique. not going to be easy. :)
> time, i think, for a think in the shower.


/* This file is part of the KDE libraries

    Copyright (C) 1999 Matthias Ettrich (ettr...@kde.org)
    Copyright (c) 2007      by Charles Connell <char...@connells.org>
    Copyright (C) 2008 Lukas Appelhans <l.appelh...@gmx.de>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "KNotificationIcon.h"
#include "kaboutdata.h"
#include "kaction.h"
#include "kcomponentdata.h"
#include "klocale.h"
#include "kmenu.h"
#include "kmessagebox.h"
#include "kshortcut.h"
#include "kactioncollection.h"
#include "kstandardaction.h"
#include <kwindowsystem.h>

#ifdef Q_WS_X11
#include <QX11Info>
#endif
#ifdef Q_WS_WIN
#include <windows.h>
#endif

#include <kiconloader.h>
#include <kapplication.h>
#include <kconfiggroup.h>

#include <QMouseEvent>
#include <QToolButton>
#include <QMovie>
#include <QPointer>

class KNotificationIconPrivate
{
public:
    KNotificationIconPrivate(KNotificationIcon *icon)
         : systemTrayIcon(icon)
    {}

    KActionCollection* actionCollection;
    KMenu* menu;
    QWidget* window;
    QAction* titleAction;
    KNotificationIcon *systemTrayIcon;
};

KNotificationIcon::KNotificationIcon( QWidget* parent )
    : QObject( parent ),
      d( new KNotificationIconPrivate( this ) )
{
    d->systemTrayIcon = new KSystemTrayIcon(parent);
}

KNotificationIcon::KNotificationIcon( const QString& icon, QWidget* parent )
    : QSystemTrayIcon( loadIcon( icon ), parent ),
      d( new KNotificationIconPrivate( this ) )
{
    d->systemTrayIcon = new KSystemTrayIcon(icon, parent);
}

KNotificationIcon::KNotificationIcon( const QIcon& icon, QWidget* parent )
    : QSystemTrayIcon( icon, parent ),
      d( new KNotificationIconPrivate( this ) )
{
    d->systemTrayIcon = new KSystemTrayIcon(icon, parent);
}

KNotificationIcon::KNotificationIcon(QMovie* movie, QWidget *parent)
    : QSystemTrayIcon(parent),
      d( new KNotificationIconPrivate( this, parent ) )
{
    d->systemTrayIcon = new KSystemTrayIcon(movie, parent);
    setMovie(movie);
}

void KNotificationIcon::init( QWidget* parent )
{
    connect(d->systemTrayIcon, SIGNAL(quitSelected()), this SIGNAL(quitSelected()));
}

QWidget *KNotificationIcon::parentWidget() const
{
    return d->window;
}

KNotificationIcon::~KNotificationIcon()
{
    delete d;
    KGlobal::deref();
}

void KNotificationIcon::contextMenuAboutToShow( )
{
    d->systemTrayIcon->contextMenuAboutToShow();
}


void KNotificationIcon::minimizeRestoreAction()
{
    if ( d->window )
    {
        d->systemTrayIcon->minimizeRestoreAction()
    }
}

void KNotificationIcon::maybeQuit()
{
    d->systemTrayIcon->maybeQuit();
}

// if the window is not the active one, show it if needed, and activate it
// (just like taskbar); otherwise hide it
void KNotificationIcon::activateOrHide( QSystemTrayIcon::ActivationReason reasonCalled )
{
    d->systemTrayIcon->activateOrHide( reasonCalled )
}

void KNotificationIcon::minimizeRestore( bool restore )
{
    d->systemTrayIcon->minimizeRestore( restore )
}

KActionCollection* KNotificationIcon::actionCollection()
{
    return d->actionCollection;
}

QIcon KNotificationIcon::loadIcon(const QString &icon, const KComponentData &componentData)
{
    return KIcon(icon);
}

void KNotificationIcon::toggleActive()
{
    activateOrHide( QSystemTrayIcon::Trigger );
}

bool KNotificationIcon::parentWidgetTrayClose() const
{
    if( kapp != NULL && kapp->sessionSaving())
        return false; // normal close
    return true;
}

void KNotificationIcon::setContextMenuTitle(QAction *action)
{
    // can never be null, and is always the requsted type, so no need to do null checks after casts.
    QToolButton *button = static_cast<QToolButton*>((static_cast<QWidgetAction*>(d->titleAction))->defaultWidget());
    button->setDefaultAction(action);
}

QAction *KNotificationIcon::contextMenuTitle() const
{
    QToolButton *button = static_cast<QToolButton*>((static_cast<QWidgetAction*>(d->titleAction))->defaultWidget());
    return button->defaultAction();
}

void KNotificationIcon::setMovie(QMovie* m)
{
    d->systemTrayIcon->setMovie(m);
}

const QMovie* KNotificationIcon::movie() const
{
    return d->systemTrayIcon->movie();
}

#include "knotificationicon.moc"
/* This file is part of the KDE libraries

   Copyright (C) 1999 Matthias Ettrich (ettr...@kde.org)
   Copyright (c) 2007      by Charles Connell <char...@connells.org>
   Copyright (C) 2008 Lukas Appelhans <l.appelh...@gmx.de>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/
#ifndef KNOTIFICATIONICON_H
#define KNOTIFICATIONICON_H


#include <QObject>

class KActionCollection;
class KNotificationIconPrivate;
class QAction;
class QMovie;

/**
 * \brief %KDE System Tray Window class
 *
 * This class implements system tray windows.
 *
 * A tray window is a small window (typically 22x22 pixel) that docks
 * into the system tray in the desktop panel. It usually displays an
 * icon or an animated icon there. The icon represents
 * the application, similar to a taskbar button, but consumes less
 * screen space.
 *
 * When the user clicks with the left mouse button on the icon, the
 * main application window is shown/raised and activated. With the
 * right mouse button, she gets a popupmenu with application specific
 * commands, including "Minimize/Restore" and "Quit".
 *
 * This is aimed to be source compatible with KSystemTrayIcon
 **/
class KDEUI_EXPORT KNotificationIcon : public QObject 
{
    Q_OBJECT
public:
    /**
     * Construct a system tray icon.
     *
     * The parent widget @p parent has a special meaning:
     * Besides owning the tray window, the parent widget will
     * dissappear from taskbars when it is iconified while the tray
     * window is visible. This is the desired behavior. After all,
     * the tray window @p is the parent's taskbar icon.
     *
     * Furthermore, the parent widget is shown or raised respectively
     * when the user clicks on the tray window with the left mouse
     * button.
     **/
    explicit KNotificationIcon( QWidget* parent = 0 );

    /**
     * Same as above but allows one to define the icon by name that should
     * be used for the system tray icon.
     */
    explicit KNotificationIcon( const QString& icon, QWidget* parent = 0 );

    /**
     * Same as above but allows one to define the icon by name that should
     * be used for the system tray icon.
     */
    explicit KNotificationIcon( const QIcon& icon, QWidget* parent = 0 );

    /**
     * Same as above but allows one to define the movie by QMovie that should
     * be used for the system tray icon. Memory management for the movie will
     * be handled by KNotificationIcon.
     */
    explicit KNotificationIcon(QMovie* movie, QWidget* parent);

    /**
     * Destructor
     */
    ~KNotificationIcon();

    /**
     * Set the movie to use. To manipulate the movie (start, stop, pause), call
     * @see movie() and make calls on the QMovie* that it returns.
     * Memory management for the movie will be handled by KNotificationIcon.
     * @since 4.2
     */
    void setMovie(QMovie* movie);

    /**
     * Get a pointer to the movie. Use this pointer to manipulate the movie
     * (start, stop, pause).
     * Will return null if no movie has been set
     * @since 4.2
     */
    const QMovie* movie() const;

    /**
       Easy access to the actions in the context menu
       Currently includes KStandardAction::Quit and minimizeRestore
    */
    KActionCollection* actionCollection();

    /** 
       Returns the QWidget set by the constructor
    */
    QWidget *parentWidget() const;

    /**
       Function to be used from function handling closing of the window associated 
       with the tray icon (i.e. QWidget::closeEvent(), KMainWindow::queryClose() or 
       similar). When false is returned, the window closing should proceed normally, 
       when true is returned, special systray-related handling should take place.
     */
    bool parentWidgetTrayClose() const;

    /**
     * Loads an icon @p icon using the icon loader class of the given componentData @p componentData.
     * The icon is applied the panel effect as it should only be used to be shown in the
     * system tray.
     * It's commonly used in the form : systray->setPixmap( systray->loadIcon( "mysystray" ) );
     */
    static QIcon loadIcon(const QString &icon, const KComponentData &componentData = KGlobal::mainComponent());

    /**
     * Sets the context menu title action to @p action.
     * The following code shows how to change the current title.
     * <code>
     * QAction *titleAction = contextMenuTitle();
     * titleAction->setText("New Title");
     * setContextMenuTitle(titleAction);
     * </code>
     */
    void setContextMenuTitle(QAction *action);

    /**
     * Returns the context menu title action.
     */
    QAction *contextMenuTitle() const;
//TODO:wrap qsystemtrayicon stuff
Q_SIGNALS:
    /**
     * Emitted when quit is selected in the menu. If you want to perform any other
     * action than to close the main application window please connect to this signal.
     */
    void quitSelected();

public Q_SLOTS:
    void toggleActive();

private Q_SLOTS:
    void contextMenuAboutToShow();
    void minimizeRestoreAction();
    void maybeQuit();
    void activateOrHide( QSystemTrayIcon::ActivationReason reasonCalled );

private:
    void init( QWidget* );
    void minimizeRestore( bool restore );

    KNotificationIconPrivate* const d;
};

#endif

_______________________________________________
Plasma-devel mailing list
Plasma-devel@kde.org
https://mail.kde.org/mailman/listinfo/plasma-devel

Reply via email to