* added first version of a simple addressbook svn path=/trunk/kdenetwork/kmail/; revision=3632wilder-work
parent
e21970c315
commit
f8397ccf9c
15 changed files with 532 additions and 29 deletions
@ -0,0 +1,133 @@ |
||||
// kmaddrbook.cpp
|
||||
// Author: Stefan Taferner <taferner@kde.org>
|
||||
// This code is under GPL
|
||||
|
||||
#include "kmaddrbook.h" |
||||
#include <kapp.h> |
||||
#include <qfile.h> |
||||
#include <assert.h> |
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
KMAddrBook::KMAddrBook(): KMAddrBookInherited() |
||||
{ |
||||
mModified = FALSE; |
||||
} |
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
KMAddrBook::~KMAddrBook() |
||||
{ |
||||
if (mModified) store(); |
||||
writeConfig(FALSE); |
||||
} |
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void KMAddrBook::insert(const QString aAddress) |
||||
{ |
||||
if (find(aAddress)<0) |
||||
{ |
||||
inSort(aAddress); |
||||
mModified=TRUE; |
||||
} |
||||
} |
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void KMAddrBook::remove(const QString aAddress) |
||||
{ |
||||
remove(aAddress); |
||||
mModified=TRUE; |
||||
} |
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void KMAddrBook::clear(void) |
||||
{ |
||||
KMAddrBookInherited::clear(); |
||||
mModified=TRUE; |
||||
} |
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void KMAddrBook::writeConfig(bool aWithSync) |
||||
{ |
||||
KConfig* config = kapp->getConfig(); |
||||
|
||||
config->setGroup("Addressbook"); |
||||
config->writeEntry("default", mDefaultFileName); |
||||
|
||||
if (aWithSync) config->sync(); |
||||
} |
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void KMAddrBook::readConfig(void) |
||||
{ |
||||
KConfig* config = kapp->getConfig(); |
||||
config->setGroup("Addressbook"); |
||||
|
||||
mDefaultFileName = config->readEntry("default"); |
||||
if (mDefaultFileName.isEmpty()) |
||||
mDefaultFileName = kapp->localkdedir()+"/share/apps/kmail/addressbook"; |
||||
|
||||
debug("addressbook: %s", mDefaultFileName.data()); |
||||
} |
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
int KMAddrBook::load(const char* aFileName) |
||||
{ |
||||
char line[256]; |
||||
const char* fname = (aFileName ? aFileName : (const char*)mDefaultFileName); |
||||
QFile file(fname); |
||||
|
||||
assert(fname != NULL); |
||||
|
||||
if (!file.open(IO_ReadOnly)) return file.status(); |
||||
clear(); |
||||
|
||||
while (!file.atEnd()) |
||||
{ |
||||
if (file.readLine(line,255) < 0) return file.status(); |
||||
if (line[strlen(line)-1] < ' ') line[strlen(line)-1] = '\0'; |
||||
if (line[0]!='#' && line[0]!='\0') inSort(line); |
||||
} |
||||
file.close(); |
||||
|
||||
mModified = FALSE; |
||||
return IO_Ok; |
||||
} |
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
int KMAddrBook::store(const char* aFileName) |
||||
{ |
||||
char* addr; |
||||
const char* fname = (aFileName ? aFileName : (const char*)mDefaultFileName); |
||||
QFile file(fname); |
||||
|
||||
assert(fname != NULL); |
||||
|
||||
if (!file.open(IO_ReadWrite|IO_Truncate)) return file.status(); |
||||
|
||||
addr = "# kmail addressbook file\n"; |
||||
if (file.writeBlock(addr,strlen(addr)) < 0) return file.status(); |
||||
|
||||
for (addr=first(); addr; addr=next()) |
||||
{ |
||||
if (file.writeBlock(addr,strlen(addr)) < 0) return file.status(); |
||||
file.writeBlock("\n",1); |
||||
} |
||||
file.close(); |
||||
|
||||
mModified = FALSE; |
||||
return IO_Ok; |
||||
} |
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
int KMAddrBook::compareItems(GCI aItem1, GCI aItem2) |
||||
{ |
||||
return strcasecmp((const char*)aItem1, (const char*)aItem2); |
||||
} |
||||
@ -0,0 +1,58 @@ |
||||
/* Simple Addressbook for KMail
|
||||
* Author: Stefan Taferner <taferner@kde.org> |
||||
* This code is under GPL |
||||
*/ |
||||
#ifndef KMAddrBook_h |
||||
#define KMAddrBook_h |
||||
|
||||
#include <qstrlist.h> |
||||
|
||||
#define KMAddrBookInherited QStrList |
||||
class KMAddrBook: protected QStrList |
||||
{ |
||||
public: |
||||
KMAddrBook(); |
||||
virtual ~KMAddrBook(); |
||||
|
||||
/** Insert given address to the addressbook. Sorted. Duplicate
|
||||
addresses are not inserted. */ |
||||
virtual void insert(const QString address); |
||||
|
||||
/** Remove given address from the addressbook. */ |
||||
virtual void remove(const QString address); |
||||
|
||||
/** Returns first address in addressbook or NULL if addressbook is empty. */ |
||||
virtual const char* first(void) { return KMAddrBookInherited::first(); } |
||||
|
||||
/** Returns next address in addressbook or NULL. */ |
||||
virtual const char* next(void) { return KMAddrBookInherited::next(); } |
||||
|
||||
/** Clear addressbook (remove the contents). */ |
||||
virtual void clear(void); |
||||
|
||||
/** Open addressbook file and read contents. The default addressbook
|
||||
file is used if no filename is given. |
||||
Returns IO_Ok on success and an IO device status on failure -- see |
||||
QIODevice::status(). */ |
||||
virtual int load(const char* fileName=NULL); |
||||
|
||||
/** Store addressbook in file or in same file of last load() call
|
||||
if no filename is given. Returns IO_Ok on success and an IO device |
||||
status on failure -- see QIODevice::status(). */ |
||||
virtual int store(const char* fileName=NULL); |
||||
|
||||
/** Read/write configuration options. Uses the group "Addressbook"
|
||||
in the app's config file. */ |
||||
virtual void readConfig(void); |
||||
virtual void writeConfig(bool withSync=TRUE); |
||||
|
||||
/** Test if the addressbook has unsaved changes. */ |
||||
virtual bool modified(void) const { return mModified; } |
||||
|
||||
protected: |
||||
virtual int compareItems(GCI item1, GCI item2); |
||||
QString mDefaultFileName; |
||||
bool mModified; |
||||
}; |
||||
|
||||
#endif /*KMAddrBook_h*/ |
||||
@ -0,0 +1,196 @@ |
||||
// kmaddrbookdlg.cpp
|
||||
// Author: Stefan Taferner <taferner@kde.org>
|
||||
|
||||
#include "kmaddrbookdlg.h" |
||||
#include "kmaddrbook.h" |
||||
#include <assert.h> |
||||
#include <klocale.h> |
||||
#include <kapp.h> |
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
KMAddrBookSelDlg::KMAddrBookSelDlg(KMAddrBook* aAddrBook, const char* aCap): |
||||
KMAddrBookSelDlgInherited(NULL, aCap, TRUE), mGrid(this, 2, 2), |
||||
mListBox(this), |
||||
mBtnOk(klocale->translate("Ok"),this),
|
||||
mBtnCancel(klocale->translate("Cancel"),this) |
||||
{ |
||||
const char* addr; |
||||
|
||||
initMetaObject(); |
||||
|
||||
assert(aAddrBook != NULL); |
||||
mAddrBook = aAddrBook; |
||||
mAddress = 0; |
||||
|
||||
mBtnOk.adjustSize(); |
||||
mBtnOk.setMinimumSize(mBtnOk.size()); |
||||
mBtnCancel.adjustSize(); |
||||
mBtnCancel.setMinimumSize(mBtnCancel.size()); |
||||
|
||||
mGrid.addMultiCellWidget(&mListBox, 0, 0, 0, 1); |
||||
mGrid.addWidget(&mBtnOk, 1, 0); |
||||
mGrid.addWidget(&mBtnCancel, 1, 1); |
||||
|
||||
mGrid.setRowStretch(0,10); |
||||
mGrid.setRowStretch(1,0); |
||||
mGrid.setColStretch(0,10); |
||||
mGrid.setColStretch(1,10); |
||||
mGrid.activate(); |
||||
|
||||
connect(&mBtnOk, SIGNAL(clicked()), SLOT(slotOk())); |
||||
connect(&mBtnCancel, SIGNAL(clicked()), SLOT(slotCancel())); |
||||
|
||||
for (addr=mAddrBook->first(); addr; addr=mAddrBook->next()) |
||||
{ |
||||
mListBox.insertItem(addr); |
||||
} |
||||
} |
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
KMAddrBookSelDlg::~KMAddrBookSelDlg() |
||||
{ |
||||
} |
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void KMAddrBookSelDlg::slotOk() |
||||
{ |
||||
int idx = mListBox.currentItem(); |
||||
|
||||
if (idx>=0) mAddress = mListBox.text(idx); |
||||
else mAddress = 0; |
||||
|
||||
accept(); |
||||
} |
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void KMAddrBookSelDlg::slotCancel() |
||||
{ |
||||
mAddress = 0; |
||||
reject(); |
||||
} |
||||
|
||||
|
||||
//=============================================================================
|
||||
//
|
||||
// Class KMAddrBookEditDlg
|
||||
//
|
||||
//=============================================================================
|
||||
|
||||
KMAddrBookEditDlg::KMAddrBookEditDlg(KMAddrBook* aAddrBook, const char* aCap): |
||||
KMAddrBookEditDlgInherited(NULL, aCap, TRUE), mGrid(this, 3, 4), |
||||
mListBox(this), mEdtAddress(this), |
||||
mBtnOk(klocale->translate("Ok"),this),
|
||||
mBtnCancel(klocale->translate("Cancel"),this), |
||||
mBtnAdd(klocale->translate("Add"),this),
|
||||
mBtnRemove(klocale->translate("Remove"),this) |
||||
{ |
||||
const char* addr; |
||||
|
||||
initMetaObject(); |
||||
|
||||
assert(aAddrBook != NULL); |
||||
mAddrBook = aAddrBook; |
||||
|
||||
mEdtAddress.adjustSize(); |
||||
mEdtAddress.setMinimumSize(mEdtAddress.size()); |
||||
mBtnOk.adjustSize(); |
||||
mBtnOk.setMinimumSize(mBtnOk.size()); |
||||
mBtnCancel.adjustSize(); |
||||
mBtnCancel.setMinimumSize(mBtnCancel.size()); |
||||
mBtnAdd.adjustSize(); |
||||
mBtnAdd.setMinimumSize(mBtnAdd.size()); |
||||
mBtnRemove.adjustSize(); |
||||
mBtnRemove.setMinimumSize(mBtnRemove.size()); |
||||
|
||||
mGrid.addMultiCellWidget(&mListBox, 0, 0, 0, 3); |
||||
mGrid.addMultiCellWidget(&mEdtAddress, 1, 1, 0, 3); |
||||
mGrid.addWidget(&mBtnOk, 2, 0); |
||||
mGrid.addWidget(&mBtnCancel, 2, 1); |
||||
mGrid.addWidget(&mBtnAdd, 2, 2); |
||||
mGrid.addWidget(&mBtnRemove, 2, 3); |
||||
|
||||
mGrid.setRowStretch(0,10); |
||||
mGrid.setRowStretch(1,0); |
||||
mGrid.setRowStretch(2,0); |
||||
mGrid.setColStretch(0,10); |
||||
mGrid.setColStretch(1,10); |
||||
mGrid.setColStretch(2,10); |
||||
mGrid.setColStretch(3,10); |
||||
mGrid.activate(); |
||||
|
||||
connect(&mListBox, SIGNAL(highlighted(const char*)),
|
||||
SLOT(slotLbxHighlighted(const char*))); |
||||
connect(&mBtnOk, SIGNAL(clicked()), SLOT(slotOk())); |
||||
connect(&mBtnCancel, SIGNAL(clicked()), SLOT(slotCancel())); |
||||
connect(&mBtnAdd, SIGNAL(clicked()), SLOT(slotAdd())); |
||||
connect(&mBtnRemove, SIGNAL(clicked()), SLOT(slotRemove())); |
||||
|
||||
for (addr=mAddrBook->first(); addr; addr=mAddrBook->next()) |
||||
{ |
||||
mListBox.insertItem(addr); |
||||
} |
||||
} |
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
KMAddrBookEditDlg::~KMAddrBookEditDlg() |
||||
{ |
||||
} |
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void KMAddrBookEditDlg::slotLbxHighlighted(const char* aItem) |
||||
{ |
||||
mEdtAddress.setText(aItem); |
||||
} |
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void KMAddrBookEditDlg::slotOk() |
||||
{ |
||||
int idx, num; |
||||
const char* addr; |
||||
|
||||
mAddrBook->clear(); |
||||
num = mListBox.count(); |
||||
for(idx=0; idx<num; idx++) |
||||
{ |
||||
addr = mListBox.text(idx); |
||||
mAddrBook->insert(addr); |
||||
} |
||||
mAddrBook->store(); |
||||
|
||||
accept(); |
||||
} |
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void KMAddrBookEditDlg::slotCancel() |
||||
{ |
||||
reject(); |
||||
} |
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void KMAddrBookEditDlg::slotAdd() |
||||
{ |
||||
const char* addr = mEdtAddress.text(); |
||||
|
||||
if (!addr || !*addr) return; |
||||
mListBox.insertItem(addr, mListBox.currentItem()); |
||||
} |
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void KMAddrBookEditDlg::slotRemove() |
||||
{ |
||||
int idx = mListBox.currentItem(); |
||||
if (idx >= 0) mListBox.removeItem(idx); |
||||
} |
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
#include "kmaddrbookdlg.moc" |
||||
@ -0,0 +1,67 @@ |
||||
/* KMail Addressbook selection and edit dialog classes
|
||||
* Author: Stefan Taferner <taferner@kde.org> |
||||
* This code is under GPL |
||||
*/ |
||||
#ifndef kmaddrbookdlg_h |
||||
#define kmaddrbookdlg_h |
||||
|
||||
#include <qdialog.h> |
||||
#include <qpushbt.h> |
||||
#include <qlistbox.h> |
||||
#include <qlined.h> |
||||
#include <qlayout.h> |
||||
|
||||
class KMAddrBook; |
||||
|
||||
#define KMAddrBookSelDlgInherited QDialog |
||||
class KMAddrBookSelDlg: public QDialog |
||||
{ |
||||
Q_OBJECT |
||||
public: |
||||
KMAddrBookSelDlg(KMAddrBook* addrBook, const char* caption); |
||||
virtual ~KMAddrBookSelDlg(); |
||||
|
||||
/** returns selected address or NULL if none was selected or the cancel
|
||||
button was pressed. */ |
||||
virtual const QString address(void) const { return mAddress; } |
||||
|
||||
protected slots: |
||||
void slotOk(); |
||||
void slotCancel(); |
||||
|
||||
protected: |
||||
KMAddrBook* mAddrBook; |
||||
QGridLayout mGrid; |
||||
QListBox mListBox; |
||||
QPushButton mBtnOk, mBtnCancel; |
||||
QString mAddress; |
||||
}; |
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#define KMAddrBookEditDlgInherited QDialog |
||||
class KMAddrBookEditDlg: public QDialog |
||||
{ |
||||
Q_OBJECT |
||||
public: |
||||
KMAddrBookEditDlg(KMAddrBook* addrBook, const char* caption); |
||||
virtual ~KMAddrBookEditDlg(); |
||||
|
||||
protected slots: |
||||
void slotOk(); |
||||
void slotCancel(); |
||||
void slotAdd(); |
||||
void slotRemove(); |
||||
void slotLbxHighlighted(const char* item); |
||||
|
||||
protected: |
||||
KMAddrBook* mAddrBook; |
||||
QGridLayout mGrid; |
||||
QListBox mListBox; |
||||
QLineEdit mEdtAddress; |
||||
QPushButton mBtnOk, mBtnCancel, mBtnAdd, mBtnRemove; |
||||
}; |
||||
|
||||
|
||||
#endif /*kmaddrbookdlg_h*/ |
||||
Loading…
Reference in new issue