• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

Nepomuk-Core

  • sources
  • kde-4.12
  • kdelibs
  • nepomuk-core
  • services
  • filewatch
activefilequeue.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the Nepomuk KDE project.
3  Copyright (C) 2011 Sebastian Trueg <trueg@kde.org>
4  Copyright (C) 2013 Vishesh Handa <me@vhanda.in>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) version 3, or any
10  later version accepted by the membership of KDE e.V. (or its
11  successor approved by the membership of KDE e.V.), which shall
12  act as a proxy defined in Section 6 of version 3 of the license.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #include "activefilequeue.h"
24 
25 #include <QtCore/QQueue>
26 #include <QtCore/QHash>
27 #include <QtCore/QTimer>
28 
29 
30 namespace {
31  class Entry {
32  public:
33  Entry(const QString& url, int c);
34  bool operator==(const Entry& other) const;
35 
37  QString url;
39  int cnt;
40  };
41 
42  Entry::Entry(const QString &u, int c)
43  : url(u),
44  cnt(c)
45  {
46  }
47 
48  bool Entry::operator==(const Entry &other) const
49  {
50  // we ignore the counter since we need this for the search in queueUrl only
51  return url == other.url;
52  }
53 }
54 
55 
56 Q_DECLARE_TYPEINFO(Entry, Q_MOVABLE_TYPE);
57 
58 
59 class ActiveFileQueue::Private
60 {
61 public:
62  QQueue<Entry> m_queue;
63  int m_queueTimeout;
64 
65  QTimer m_queueTimer;
66 
68  QHash<QString, int> m_emittedEntries;
69  int m_emittedTimeout;
70 
71 };
72 
73 
74 ActiveFileQueue::ActiveFileQueue(QObject *parent)
75  : QObject(parent),
76  d(new Private())
77 {
78  // we default to 5 seconds
79  d->m_queueTimeout = 5;
80  d->m_emittedTimeout = 5;
81 
82  // setup the timer
83  connect(&d->m_queueTimer, SIGNAL(timeout()),
84  this, SLOT(slotTimer()));
85 
86  // we check in 1 sec intervals
87  d->m_queueTimer.setInterval(1000);
88 }
89 
90 ActiveFileQueue::~ActiveFileQueue()
91 {
92  delete d;
93 }
94 
95 void ActiveFileQueue::enqueueUrl(const QString& url)
96 {
97  Entry defaultEntry(url, d->m_queueTimeout);
98 
99  // If the url is already in the queue update its timestamp
100  QQueue<Entry>::iterator it = qFind(d->m_queue.begin(), d->m_queue.end(), defaultEntry);
101  if(it != d->m_queue.end()) {
102  it->cnt = d->m_queueTimeout;
103  }
104  else {
105  // We check if we just emitted the url, if so we move it to the normal queue
106  QHash<QString, int>::iterator iter = d->m_emittedEntries.find(url);
107  if( iter != d->m_emittedEntries.end() ) {
108  d->m_queue.enqueue( defaultEntry );
109  d->m_emittedEntries.erase( iter );
110  }
111  else {
112  // It's not in any of the queues
113  emit urlTimeout( url );
114  d->m_emittedEntries.insert( url, d->m_emittedTimeout );
115  }
116  }
117 
118  // make sure the timer is running
119  if(!d->m_queueTimer.isActive()) {
120  d->m_queueTimer.start();
121  }
122 }
123 
124 void ActiveFileQueue::setTimeout(int seconds)
125 {
126  d->m_queueTimeout = seconds;
127 }
128 
129 void ActiveFileQueue::setWaitTimeout(int seconds)
130 {
131  d->m_emittedTimeout = seconds;
132 }
133 
134 void ActiveFileQueue::slotTimer()
135 {
136  // we run through the queue, decrease each counter and emit each entry which has a count of 0
137  QMutableListIterator<Entry> it( d->m_queue );
138  while( it.hasNext() ) {
139  Entry& entry = it.next();
140  entry.cnt--;
141  if( entry.cnt <= 0 ) {
142  // Insert into the emitted queue
143  d->m_emittedEntries.insert( entry.url, d->m_emittedTimeout );
144 
145  emit urlTimeout( entry.url );
146  it.remove();
147  }
148  }
149 
150  // Run through all the emitted entires and remove them
151  QMutableHashIterator<QString, int> iter( d->m_emittedEntries );
152  while( iter.hasNext() ) {
153  iter.next();
154  iter.value()--;
155  if( iter.value() <= 0 ) {
156  iter.remove();
157  }
158  }
159 
160  // stop the timer in case we have nothing left to do
161  if(d->m_queue.isEmpty() && d->m_emittedEntries.isEmpty()) {
162  d->m_queueTimer.stop();
163  }
164 }
165 
166 #include "activefilequeue.moc"
ActiveFileQueue::ActiveFileQueue
ActiveFileQueue(QObject *parent=0)
Definition: activefilequeue.cpp:74
ActiveFileQueue::~ActiveFileQueue
~ActiveFileQueue()
Definition: activefilequeue.cpp:90
activefilequeue.h
ActiveFileQueue::setWaitTimeout
void setWaitTimeout(int seconds)
Sets the timeout in seconds, that the queue should track the url once it has already been emitted...
Definition: activefilequeue.cpp:129
QHash
Q_DECLARE_TYPEINFO
Q_DECLARE_TYPEINFO(Entry, Q_MOVABLE_TYPE)
QObject
ActiveFileQueue::setTimeout
void setTimeout(int seconds)
Set the timeout in seconds.
Definition: activefilequeue.cpp:124
ActiveFileQueue::enqueueUrl
void enqueueUrl(const QString &url)
Definition: activefilequeue.cpp:95
ActiveFileQueue::urlTimeout
void urlTimeout(const QString &url)
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:08 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Nepomuk-Core

Skip menu "Nepomuk-Core"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal