⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 categories.cpp

📁 Trolltech公司发布的图形界面操作系统。可在qt-embedded-2.3.10平台上编译为嵌入式图形界面操作系统。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/************************************************************************ Copyright (C) 2000-2005 Trolltech AS.  All rights reserved.**** This file is part of the Qtopia Environment.** ** This program is free software; you can redistribute it and/or modify it** under the terms of the GNU General Public License as published by the** Free Software Foundation; either version 2 of the License, or (at your** option) any later version.** ** A copy of the GNU GPL license version 2 is included in this package as ** LICENSE.GPL.**** 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.**** In addition, as a special exception Trolltech gives permission to link** the code of this program with Qtopia applications copyrighted, developed** and distributed by Trolltech under the terms of the Qtopia Personal Use** License Agreement. You must comply with the GNU General Public License** in all respects for all of the code used other than the applications** licensed under the Qtopia Personal Use License Agreement. If you modify** this file, you may extend this exception to your version of the file,** but you are not obligated to do so. If you do not wish to do so, delete** this exception statement from your version.** ** See http://www.trolltech.com/gpl/ for GPL licensing information.**** Contact info@trolltech.com if any conditions of this licensing are** not clear to you.************************************************************************/#define QTOPIA_INTERNAL_FILEOPERATIONS#include <qtopia/categories.h>#include <qtopia/stringutil.h>#include <qtopia/global.h>#include "localtr_p.h"#include <qfile.h>#include <qdir.h>#include <qfileinfo.h>#include <qcstring.h>#include <qtextstream.h>#ifdef Q_WS_QWS#include <qtopia/qcopenvelope_qws.h>#endifusing namespace Qtopia;/*********************************************************** * * CategoryGroup * **********************************************************/#ifdef QTOPIA_DESKTOPUidGen CategoryGroup::sUidGen( UidGen::PalmtopCenter );#elseUidGen CategoryGroup::sUidGen( UidGen::Qtopia );#endif/*! \class CategoryGroup categories.h  \brief The CategoryGroup class is a group of categories.  CategoryGroup is a group of categories that is associated with an  application or global set. Mainly it defines a map of ids to  category labels and category labels to ids. Lookups can be done with  labels or unique idenifiers.  This is mainly an internal class used by Categories. Categories  does return this class in it's Categories::appGroupMap() and  Categories::globalGroup().  \ingroup qtopiaemb  \ingroup qtopiadesktop  \warning Categories API will likely change for Qtopia 2. *//*! Add \a label and return the UID. If failure, then 0 is returned. Note  that All and Unfiled are reserved labels.  \internal*/int CategoryGroup::add( const QString &label ){    if ( label == "_All" || label == "_Unfiled" )	return 0;    QMap<QString,int>::Iterator findIt = mLabelIdMap.find( label );    if ( findIt != mLabelIdMap.end() )	return 0;    int newUid = uidGen().generate();    insert( newUid, label );    return newUid;}void CategoryGroup::insert( int uid, const QString &label ){    uidGen().store( uid );    mIdLabelMap[uid] = label;    mLabelIdMap[label] = uid;}/*! \internal */bool CategoryGroup::add( int uid, const QString &label ){    if ( label == "_All" || label == "_Unfiled" )	return FALSE;    QMap<QString,int>::ConstIterator labelIt = mLabelIdMap.find( label );    if ( labelIt != mLabelIdMap.end() )	return FALSE;    QMap<int,QString>::ConstIterator idIt = mIdLabelMap.find( uid );    if ( idIt != mIdLabelMap.end() )	return FALSE;    insert( uid, label );    return TRUE;}/*! Returns TRUE if \a label was removed from the group, FALSE if not.  \internal */bool CategoryGroup::remove( const QString &label ){    QMap<QString,int>::Iterator findIt = mLabelIdMap.find( label );    if ( findIt == mLabelIdMap.end() )	return FALSE;    mIdLabelMap.remove( *findIt );    mLabelIdMap.remove( findIt );    return TRUE;}/*! Returns TRUE if \a uid was removed from the group, FALSE if not.  \internal */bool CategoryGroup::remove( int uid ){    QMap<int,QString>::Iterator idIt = mIdLabelMap.find( uid );    if ( idIt == mIdLabelMap.end() )	return FALSE;    mLabelIdMap.remove( *idIt );    mIdLabelMap.remove( idIt );    return TRUE;}/*! \internal */bool CategoryGroup::rename( int uid, const QString &newLabel ){    if ( newLabel == "_All" || newLabel == "_Unfiled" )	return FALSE;    QMap<int, QString>::Iterator idIt = mIdLabelMap.find( uid );    if ( idIt == mIdLabelMap.end() )	return FALSE;    // check for name conflict    for ( QMap<int, QString>::Iterator it = mIdLabelMap.begin(); it != mIdLabelMap.end(); ++it) {	if ( it != idIt && (*it) == newLabel )	    return FALSE;    }    mLabelIdMap.remove( *idIt );    mLabelIdMap[newLabel] = uid;    *idIt = newLabel;    return TRUE;}/*! \internal */bool CategoryGroup::rename( const QString &oldLabel, const QString &newLabel ){    return rename( id(oldLabel), newLabel );}/*! Returns TRUE if \a uid is stored in this group, FALSE if not. */bool CategoryGroup::contains(int uid) const{    return ( mIdLabelMap.find( uid ) != mIdLabelMap.end() );}/*! Returns TRUE if \a label is stored in this group, FALSE if not. */bool CategoryGroup::contains(const QString &label) const{    return ( mLabelIdMap.find( label ) != mLabelIdMap.end() );}/*! Returns label associated with the \a uid or QString::null if  not found */const QString &CategoryGroup::label(int uid) const{    QMap<int,QString>::ConstIterator idIt = mIdLabelMap.find( uid );    if ( idIt == mIdLabelMap.end() )	return QString::null;    return *idIt;}/*! Returns the uid associated with \a label or 0 if not found */int CategoryGroup::id(const QString &label) const{    QMap<QString,int>::ConstIterator labelIt = mLabelIdMap.find( label );    if ( labelIt == mLabelIdMap.end() )	return 0;    return *labelIt;}/*! Returns a list of all labels stored in this group. */QStringList CategoryGroup::labels() const{    QStringList labels;    for ( QMap<int, QString>::ConstIterator it = mIdLabelMap.begin();	  it != mIdLabelMap.end(); ++it )	labels += *it;    // ### I don't think this is the place for this...//    labels.sort();    return labels;}/*! Returns a list of all labels associated with the \a catids */QStringList CategoryGroup::labels(const QArray<int> &catids ) const{    QStringList labels;    if ( catids.count() == 0 )	return labels;    for ( QMap<int, QString>::ConstIterator it = mIdLabelMap.begin();	  it != mIdLabelMap.end(); ++it )	if ( catids.find( it.key() ) != -1 )	    labels += *it;    return labels;}#ifdef Q_OS_WIN32Qtopia::UidGen & CategoryGroup::uidGen(){    return sUidGen;}#endif/*********************************************************** * * Categories * **********************************************************//*!  \class Categories categories.h  \brief The Categories class is a database that groups categories and maps ids to names.  The Categories class is the low level Categories accessor class. To  add a category menu and filter for your application, see CategoryMenu.  The Categories class allows the developer to add, remove, and rename  categories. Categories can be created for an individual application  such as Todo List or to be used for all applications. Categories  that can be used by all applications are called global  categories. Each PimRecord subclass stores categories as an  QArray<int> using PimRecord::setCategories() and  PimRecord::categories(). This allows each record to be assigned  to multiple categories. This also allows the user to rename a  category and for it to update automatically in all records.  This class provides several methods to convert between a category id  and it's associated string such as id(), ids(), label() and labels(). A  helper class called CategoryGroup is used to access categories of a  single application group, such as Todo List. Global categories can  also be accessed via CategoryGroup objects.  See appGroupMap() and  globalGroup() for the appropriate accessor methods.  Categories are stored in an xml file in the Settings directory  (Categories.xml). A global function called categoryFileName() will  return to appropriate QString file location to be passed to load()  and save() for the master categories database.  \ingroup qtopiaemb  \ingroup qtopiadesktop  \warning Categories API will likely change after Qtopia 2.  \sa CategoryGroup, CategoryMenu, PimRecord*//*!    Only unique names allowed for categories.  Returns false if the name exists    in any category    \internal*/static bool contains(const Categories *c, const QString &label){    if ( c->globalGroup().contains(label) )	return TRUE;    for ( QMap<QString, CategoryGroup>::ConstIterator it = c->appGroupMap().begin();	    it != c->appGroupMap().end(); ++it ) {	if ( (*it).contains(label) )	    return TRUE;    }    return FALSE;}/*!  Add the category name as long as it doesn't already exist locally or  globally.  The \a uid is assigned to the category if successfully  added. Return \a uid if added, 0 if conflicts (error).  \internal*/int Categories::addCategory( const QString &appname,			     const QString &catname,			     int uid ){    if ( contains(this, catname) )	return 0;    QMap< QString, CategoryGroup >::Iterator	appIt = mAppCats.find( appname );    if ( appIt == mAppCats.end() ) {	CategoryGroup newgroup;	newgroup.add( uid, catname );	mAppCats.insert( appname, newgroup );	emit categoryAdded( *this, appname, uid );	return uid;    }    CategoryGroup &cats = *appIt;    cats.add( uid, catname );    emit categoryAdded( *this, appname, uid );    return uid;}/*!  Add the category name \a catname for the application \a appname.  Return UID if added, 0 if the category already exists locally  for any application or globally.*/int Categories::addCategory( const QString &appname,			     const QString &catname ){    if ( contains(this, catname) )	return 0;    QMap< QString, CategoryGroup >::Iterator	appIt = mAppCats.find( appname );    if ( appIt == mAppCats.end() ) {	CategoryGroup newgroup;	int uid = newgroup.add( catname );	mAppCats.insert( appname, newgroup );	emit categoryAdded( *this, appname, uid );	return uid;    }    CategoryGroup &cats = *appIt;    int uid = cats.add( catname );    if ( !uid )	return 0;    emit categoryAdded( *this, appname, uid );    return uid;}/*!  \internal*/int Categories::addGlobalCategory( const QString &catname, int uid ){    mGlobalCats.add( uid, catname );    emit categoryAdded( *this, QString::null, uid );    return uid;}/*!  Add the global category \a catname while checking that it doesn't  already exist globally. Return UID if added, 0 if conflicts.  \sa addCategory() */int Categories::addGlobalCategory( const QString &catname ){    if ( contains(this, catname) )	return 0;    int uid = mGlobalCats.add( catname );    if ( !uid )	return 0;    emit categoryAdded( *this, QString::null, uid );    return uid;}/*!  Removes the \a catname from the application group. If it is not  found in the application group \a appname and \a checkGlobal is TRUE, then it  attempts to remove it from the global list*/bool Categories::removeCategory( const QString &appname,				 const QString &catname,				 bool checkGlobal ){    QMap< QString, CategoryGroup >::Iterator	appIt = mAppCats.find( appname );    if ( appIt != mAppCats.end() ) {	CategoryGroup &cats = *appIt;	int uid = cats.id( catname );	if ( cats.remove( uid ) ) {	    emit categoryRemoved( *this, appname, uid );	    return TRUE;	}    }    if ( !checkGlobal )	return FALSE;    return removeGlobalCategory( catname );}/*!  Removes the \a uid from the application group \a appname. Returns TRUE  if success, FALSE if not found.*/bool Categories::removeCategory( const QString &appname, int uid ){    QMap< QString, CategoryGroup >::Iterator	appIt = mAppCats.find( appname );    if ( appIt != mAppCats.end() ) {	CategoryGroup &cats = *appIt;	if ( cats.remove( uid ) ) {	    emit categoryRemoved( *this, appname, uid );	    return TRUE;	}    }    return FALSE;}/*!  Removes the global category \a catname. Returns TRUE  if success, FALSE if not found.*/bool Categories::removeGlobalCategory( const QString &catname ){    int uid = mGlobalCats.id( catname );    if ( mGlobalCats.remove( uid ) ) {	emit categoryRemoved( *this, QString::null, uid );	return TRUE;    }    return FALSE;}/*!  Removes the global category \a uid. Returns TRUE  if success, FALSE if not found.*/bool Categories::removeGlobalCategory( int uid ){     if ( mGlobalCats.remove( uid ) ) {	emit categoryRemoved( *this, QString::null, uid );	return TRUE;    }    return FALSE;}/*!  Returns the sorted list of all categories that are associated with  the \a app.  If \a includeGlobal is TRUE then the returned  categories will include the global category items.  \a extra specifies which extra labels should be added. */QStringList Categories::labels( const QString &app,				bool includeGlobal,				ExtraLabels extra ) const{    QMap< QString, CategoryGroup >::ConstIterator	appIt = mAppCats.find( app );    QStringList cats;    if ( appIt != mAppCats.end() )	cats += (*appIt).labels();    //else qDebug("Categories::labels didn't find app %s", app.latin1() );    if ( includeGlobal )	cats += mGlobalCats.labels();    cats.sort();    switch ( extra ) {    case NoExtra: break;    case AllUnfiled:	cats.append( "_All" );	cats.append( "_Unfiled" );	break;    case AllLabel:	cats.append( "_All" );	break;    case UnfiledLabel:	cats.append( "_Unfiled" );	break;    }    return cats;}/*!  Returns the label associated with \a id in application \a app.*/QString Categories::label( const QString &app, int id ) const{    if ( mGlobalCats.contains( id ) )	return mGlobalCats.label( id );    QMap< QString, CategoryGroup >::ConstIterator	appIt = mAppCats.find( app );    if ( appIt == mAppCats.end() )	return QString::null;    return (*appIt).label( id );}QString qpe_translateLabel(const QString& s){    if ( s[0] == '_' ) {	return LocalTranslator::translate("Categories-*","Categories",s.mid(1).latin1());    } else {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -