[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    kdebase/kicker/ui/recentapps.* -> kdelibs/kio/kio/krecentapps.*?
From:       Stephan Binner <binner () kde ! org>
Date:       2003-03-04 17:35:41
[Download RAW message or body]

Hello,

I wrote a kicker/menuext/quickstart extension which shows the last/most used 
menu items. There my problem was the synchronisation with PanelServiceMenu's 
static RecentlyLaunchedApps class. So I rewrote it a little bit and want to
ask if it can be put into kio library (has to be something to which menuext 
links) even if it doesn't fit 100% there. I also have a patch for KDesktop's 
mini-cli to use it (and trigger update of Kicker's process copy via DCOP).

Perhaps even KRun can be changed to add to it, but I fear without an over-
loaded flag "addToRecentApps" or alike too many services would be added.

Any opinion (including if slower static methods would be better) welcome.

Bye,
   Steve
["krecentapps.cpp" (text/x-c++src)]

/*****************************************************************

Copyright (c) 2000 the kicker authors. See file AUTHORS.

Permission is hereby granted, free of charge, to any person obtaining a \
copy of this software and associated documentation files (the "Software"), \
to deal in the Software without restriction, including without limitation \
the rights to use, copy, modify, merge, publish, distribute, sublicense, \
and/or sell copies of the Software, and to permit persons to whom the \
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL \
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

******************************************************************/

#include <time.h>
#include <qstringlist.h>
#include <qvaluelist.h>

#include <kglobal.h>
#include <kdebug.h>
#include <kconfig.h>
#include <krecentapps.h>
#include <kstaticdeleter.h>

#include <stdlib.h>

class KRecentAppInfo
{
    friend class KRecentApps;

protected:
    KRecentAppInfo(const int nLaunchCount, const time_t lastLaunchTime)
    {
        m_nLaunchCount = nLaunchCount;
        m_LastLaunchTime = lastLaunchTime;
    }

    int getLaunchCount() const { return m_nLaunchCount; };
    time_t getLastLaunchTime() const { return m_LastLaunchTime; };
    void increaseLaunchCount() { m_nLaunchCount++; };
    void setLaunchCount(const int nLaunchCount) { m_nLaunchCount = \
nLaunchCount; };  void setLastLaunchTime(const time_t lastLaunch) { \
m_LastLaunchTime = lastLaunch; };

private:
    int m_nLaunchCount;
    time_t m_LastLaunchTime;
};

template class KStaticDeleter<KRecentApps>;

KRecentApps* KRecentApps::s_self = 0;
KStaticDeleter<KRecentApps> krecentappssd;

KRecentApps* KRecentApps::self()
{
    if ( !s_self )
        krecentappssd.setObject( s_self, new KRecentApps );

    return s_self;
}

KRecentApps::KRecentApps()
{
    // set defaults
    m_bRecentVsOften = false;
    m_nNumVisible = 5; // max entries to display
    m_nMaxEntries = 25; // max entries to store
    m_Info.resize(m_nMaxEntries * 2);
    m_Info.setAutoDelete(TRUE);
    m_bInitialised = false;
}

void KRecentApps::init()
{
    if (m_bInitialised) return;

    KConfig *config = new KConfig("kickerrc");
    config->setGroup("menus");

    if (config->hasKey("NumVisibleEntries"))
        m_nNumVisible = config->readNumEntry("NumVisibleEntries");

    if (config->hasKey("MaxRecentAppsEntries")){
        m_nMaxEntries = config->readNumEntry("MaxRecentAppsEntries");
    }

    if (config->hasKey("RecentVsOften"))
        m_bRecentVsOften = config->readBoolEntry("RecentVsOften");

    m_Info.clear();
    if (config->hasKey("RecentAppsStat")){
        QStringList RecentApps = config->readListEntry("RecentAppsStat");
        QValueList<QString>::ConstIterator it;
        for (it = RecentApps.begin(); it != RecentApps.end(); ++it){
            const QString strInfo = (*it).stripWhiteSpace();
            if (!strInfo.isEmpty()) {
                int nCount;
                long lTime;

                int nFind = strInfo.find(" ");
                if (nFind > 0) {
                    QString strCount(strInfo.left(nFind));
                    nCount = strCount.toInt();

                    int nFind2 = strInfo.find(" ", nFind+1);
                    if (nFind2 > 0) {
                        QString strTime(strInfo.mid(nFind+1, \
nFind2-nFind-1));  lTime = strTime.toLong();

                        QString szPath = strInfo.right(strInfo.length() - \
                nFind2 - 1);
                        m_Info.insert(szPath, new KRecentAppInfo(nCount, \
(time_t)lTime));  }
                }
            }
        }
    }
    m_bInitialised = true;
}

void KRecentApps::readConfig() {
    m_bInitialised = false;
}

void KRecentApps::save()
{
    KConfig *config = new KConfig("kickerrc");
    config->setGroup("menus");

    QStringList RecentApps;
    if (!m_Info.isEmpty()) {
        QDictIterator<KRecentAppInfo> it(m_Info);
        for (KRecentAppInfo *pInfo = it.toFirst(); pInfo; ++it, pInfo = \
it.current()){  QString strPath(it.currentKey());
            QString strInfo;
            strInfo.sprintf("%d %ld ", pInfo->getLaunchCount(), \
(long)pInfo->getLastLaunchTime());  strInfo += strPath;

            RecentApps.append(strInfo);
        }
    }
    config->writeEntry("RecentAppsStat", RecentApps);
    config->sync();
}

void KRecentApps::appLaunched(const QString & strApp)
{
    if (!m_bInitialised) init();

    KRecentAppInfo *pInfo = m_Info.find(strApp);
    if (pInfo) {
        pInfo->increaseLaunchCount();
        pInfo->setLastLaunchTime(time(0));
    }
    else {
        m_Info.insert(strApp, new KRecentAppInfo( 1, time(0)));
        checkOverlimit();
    }
    save();
}

void KRecentApps::checkOverlimit()
{
    if ((int)m_Info.count() < m_nMaxEntries) return;

    QStringList strsToClear;
    QDictIterator<KRecentAppInfo> it(m_Info);

    for( int i = m_Info.count() - m_nMaxEntries; i > 0; i--)
    {
        // not most effective but easiest
        QString strFoundPath;
        KRecentAppInfo *pFoundInfo = 0;
        for (KRecentAppInfo *pInfo = it.toFirst(); pInfo; ++it, pInfo = \
it.current())  {
            QString strPath(it.currentKey());
            if (strFoundPath != strPath && strsToClear.find(strPath) == \
strsToClear.end()) {  if (!pFoundInfo) {
                    pFoundInfo = pInfo;
                    strFoundPath = strPath;
                }
                else if (m_bRecentVsOften) {
                    if (pFoundInfo->getLastLaunchTime() >= \
pInfo->getLastLaunchTime()) {  pFoundInfo = pInfo;
                        strFoundPath = strPath;
                    }
                }
                else if (pFoundInfo->getLaunchCount() > \
                pInfo->getLaunchCount() ||
                         (pFoundInfo->getLaunchCount() == \
                pInfo->getLaunchCount() &&
                          pFoundInfo->getLastLaunchTime() >= \
pInfo->getLastLaunchTime()))  {
                    pFoundInfo = pInfo;
                    strFoundPath = strPath;
                }
            }
        }
        strsToClear.append(strFoundPath);
    }
    if (!strsToClear.isEmpty()) {
        QValueList<QString>::ConstIterator it;
        for (it = strsToClear.begin(); it != strsToClear.end(); ++it)
            m_Info.remove(*it);
    }
}

void KRecentApps::getRecentApps(QStringList & RecentApps)
{
    if (!m_bInitialised) init();

    // not most effective but easiest
    QDictIterator<KRecentAppInfo> it(m_Info);

    RecentApps.clear();

    KRecentAppInfo MinInfo(0, 0);
    QString strFoundPath;
    for (int i = 0; i < m_nNumVisible && i < (int)it.count(); i++)
    {
        KRecentAppInfo *pFoundInfo = &MinInfo;
        for (KRecentAppInfo *pInfo = it.toFirst(); pInfo; ++it, pInfo = \
it.current())  {
            QString strPath(it.currentKey());
            if (strFoundPath != strPath && RecentApps.find(strPath) == \
RecentApps.end()) {  if (m_bRecentVsOften){
                    if (pFoundInfo->getLastLaunchTime() <= \
pInfo->getLastLaunchTime()){  pFoundInfo = pInfo;
                        strFoundPath = strPath;
                    }
                }
                else if (pFoundInfo->getLaunchCount() < \
                pInfo->getLaunchCount() ||
                         (pFoundInfo->getLaunchCount() == \
                pInfo->getLaunchCount() &&
                          pFoundInfo->getLastLaunchTime() <= \
pInfo->getLastLaunchTime()))  {
                    pFoundInfo = pInfo;
                    strFoundPath = strPath;
                }
            }
        }
        if (pFoundInfo != &MinInfo) // should always go through
            RecentApps.append(strFoundPath);
    }
}

void KRecentApps::removeItem(const QString &strName)
{
    if (!m_bInitialised) init();

    if (!strName.isEmpty())
        m_Info.remove(strName);
}


["krecentapps.h" (text/x-chdr)]

/*****************************************************************

Copyright (c) 2000 the kicker authors. See file AUTHORS.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

******************************************************************/

#ifndef __recentapps_h__
#define __recentapps_h__

#include <qdict.h>

class KRecentAppInfo;

class KRecentApps
{
public:
    void appLaunched(const QString & strApp);
    void getRecentApps(QStringList & RecentApps);
    void removeItem(const QString &strName);
    void readConfig();

    static KRecentApps* self();

protected:
    KRecentApps();
    void init();

private:
    static KRecentApps* s_self;

    void checkOverlimit();
    void save();

    int m_nMaxEntries;
    QDict<KRecentAppInfo> m_Info;
    bool m_bRecentVsOften;
    int m_nNumVisible;
    bool m_bInitialised;
};

#endif


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic