summaryrefslogtreecommitdiffstats
path: root/src/daemon/NotifyWidget.cpp
blob: 78d84455341acdc0ffab10a2c5e06351a3f67c36 (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
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
/*
 * NotifyWidget.cpp
 *
 * Copyright (C) 2021  Emanoil Kotsev <deloptes@gmail.com>
 *
 *
 *  This file is part of kdbusnotification.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <kdebug.h>
#include <tqtimer.h>
#include <tqdbusvariant.h>

#include "NotifyWidget.h"
#include "NotificationsService.h"

#define FADE_SPEED 5
#define EXPTIMEOUT 15000 //msec

NotifyWidget::NotifyWidget(TQWidget *parent, const char *name, NotificationsService *ns, TQ_INT32 id )
    : TQLabel( parent, name, WStyle_Customize | TQt::WStyle_StaysOnTop | TQt::WStyle_NoBorder),
      mName(TQString(name)),
      notificationService(ns),
      mId(id),
      mUrgency(0),
      mPersistence(false),
      mSuppressSound(false),
      mTransient(false),
      mSenderPid(0)
{
// TODO: give the user an option to configure if he/she wants to have
//    the notification fading away from down to top
//    TQTimer::singleShot(100, this, TQ_SLOT(fadeAway()));
    mTimer = new TQTimer( this );
    connect( mTimer, TQ_SIGNAL(timeout()), this, TQ_SLOT(slotTimeout()) );
}

NotifyWidget::~NotifyWidget()
{
    if(mTimer)
        delete mTimer;
    if(notificationService)
        delete notificationService;
}

void NotifyWidget::setAutoMask(bool b)
{
    if (b)
        setBackgroundMode( PaletteForeground );
    else
        setBackgroundMode( PaletteBackground );
    TQWidget::setAutoMask(b);
}

bool NotifyWidget::setIcon(const TQString& icon) {
    mIcon = icon;
    TQPixmap pixmap;
    if ( pixmap.load(mIcon) ) {
        this->setPixmap(pixmap);
    } else {
        tqDebug("Could not load notification icon");
        return false;
    }
    return true;
}

/**
 * The actions send a request message back to the notification client when invoked. This functionality may not be implemented by the notification server, conforming clients should check if it is available before using it (see the GetCapabilities message in Protocol). An implementation is free to ignore any requested by the client. As an example one possible rendering of actions would be as buttons in the notification popup.

Actions are sent over as a list of pairs. Each even element in the list (starting at index 0) represents the identifier for the action. Each odd element in the list is the localized string that will be displayed to the user.

The default action (usually invoked by clicking the notification) should have a key named "default". The name can be anything, though implementations are free not to display it.
 */
void NotifyWidget::setActions(const TQStringList& actions) {
    mActions = actions;
    // not implemented
}

/**
 * Hints are a way to provide extra data to a notification server that the server may be able to make use of.

Neither clients nor notification servers are required to support any hints. Both sides should assume that hints are not passed, and should ignore any hints they do not understand.
 */

/**
 * Notifications can optionally have a type indicator. Although neither client or nor server must support this, some may choose to. Those servers implementing categories may use them to intelligently display the notification in a certain way, or group notifications of similar types.
 */
void NotifyWidget::setCategory(const TQString& c) {
    mCategory = c;
}

void NotifyWidget::setPersistence(bool p) {
    mPersistence = p;
}

void NotifyWidget::setImage(const TQString& i) {
    mIcon = i;
}
void NotifyWidget::setImageData(const TQValueList<TQT_DBusData>& data) {

    int x = data[0].toInt32();
    int y = data[1].toInt32();
    int r = data[2].toInt32(); //  rowstride
    int a = data[3].toBool(); // alpha
    int b = data[4].toInt32(); // bits per sample
    int c = data[5].toInt32(); // channels
    TQValueList<TQT_DBusData> v = data[6].toTQValueList(); // image bytes
//    int w, int h, int depth, int numColors = 0, Endian bitOrder = IgnoreEndian
    mImageData = TQPixmap(x,y);
    TQByteArray i;
    TQValueList<TQT_DBusData>::Iterator it;
    TQ_UINT32 count;
    for ( it = v.begin(); it != v.end(); ++it )
        i[count++]=(*it).toByte();
    mImageData.loadFromData(i);
    setPixmap(mImageData);
}

void NotifyWidget::setSoundFile(const TQString& f) {
    mSoundFile = f;
}

void NotifyWidget::setSoundName(const TQString& n) {
    mSoundName = n;
}

void NotifyWidget::setSuppressSound(bool s) {
    mSuppressSound = s;
}

void NotifyWidget::setTransient(bool t) {
    mTransient = t;
}

void NotifyWidget::setUrgency(TQ_UINT16 l) {
    mUrgency = l;
}


void NotifyWidget::setSenderPid(TQ_UINT64 p) {
    mSenderPid = p;
}

void NotifyWidget::setTimeout(TQ_INT32 t) {
    if(t == -1)
    {
        t = EXPTIMEOUT;
    }
    if (t > 0) {
        mTimer->start( t, TRUE );
    }
}

void NotifyWidget::mousePressEvent( TQMouseEvent *e )
{
    if (e->button() == TQMouseEvent::LeftButton)
    {
        if(mTimer->isActive())
            mTimer->stop();
        notificationService->closeNotifyWidget(mId, 2); // The notification was dismissed by the user (2).
    }
}

void NotifyWidget::slotTimeout()
{
    notificationService->closeNotifyWidget(mId, 1); // The notification expired (1).
}

void NotifyWidget::slotFadeAway()
{
    TQPoint p = pos();
    p.setY(p.y()-1);
    move(p);
    TQTimer::singleShot(FADE_SPEED, this, TQ_SLOT(fadeAway()));
}

#include "NotifyWidget.moc"