summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/groupwise/libgroupwise/tasks/createcontacttask.cpp
blob: bf0873ab0250b61832930f9953e9001720ee58dd (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
/*
    Kopete Groupwise Protocol
    createcontacttask.cpp - high level task responsible for creating both a contact and any folders it belongs to locally, on the server

    Copyright (c) 2004      SUSE Linux AG	 	 http://www.suse.com
    
    Based on Iris, Copyright (C) 2003  Justin Karneges

    Kopete (c) 2002-2004 by the Kopete developers <[email protected]>
 
    *************************************************************************
    *                                                                       *
    * This library is free software; you can redistribute it and/or         *
    * modify it under the terms of the GNU Lesser General Public            *
    * License as published by the Free Software Foundation; either          *
    * version 2 of the License, or (at your option) any later version.      *
    *                                                                       *
    *************************************************************************
*/

#include "client.h"
#include "createfoldertask.h"
#include "createcontactinstancetask.h"

#include "createcontacttask.h"

CreateContactTask::CreateContactTask(Task* tqparent): Task(tqparent)
{
}

CreateContactTask::~CreateContactTask()
{
}

TQString CreateContactTask::userId()
{
	return m_userId;
}

TQString CreateContactTask::dn()
{
	return m_dn;
}

TQString CreateContactTask::displayName()
{
	return m_displayName;
}

bool CreateContactTask::take( Transfer * transfer )
{
	Q_UNUSED( transfer );
	return false;
}

void CreateContactTask::contactFromUserId( const TQString & userId, const TQString & displayName, const int firstSeqNo, const TQValueList< FolderItem > folders, bool topLevel )
{
	m_userId = userId;
	m_displayName = displayName;
	m_firstSequenceNumber = firstSeqNo;
	m_folders = folders;
	m_topLevel = topLevel;
}

void CreateContactTask::onGo()
{
	client()->debug( "CreateContactTask::onGo() - Welcome to the Create Contact Task Show!");
	TQValueList<FolderItem>::ConstIterator it = m_folders.begin();
	const TQValueList<FolderItem>::ConstIterator end = m_folders.end();
	
	// create contacts on the server
	for ( ; it != end; ++it )
	{
		client()->debug( TQString( " - contact is in folder %1 with id %2" ).tqarg( (*it).name ).tqarg( (*it).id ) );
		CreateContactInstanceTask * ccit = new CreateContactInstanceTask( client()->rootTask() );
		// the add contact action may cause other contacts' sequence numbers to change
		// CreateContactInstanceTask signals these changes, so we propagate the signal via the Client, to the GroupWiseAccount
		// This updates our local versions of those contacts using the same mechanism by which they are updated at login.
		connect( ccit, TQT_SIGNAL( gotContactAdded( const ContactItem & ) ), TQT_SLOT( slotContactAdded( const ContactItem & ) ) );
        connect( ccit, TQT_SIGNAL( finished() ), TQT_SLOT( slotCheckContactInstanceCreated() ) );
		if ( (*it).id == 0 ) // caller asserts that this isn't on the server...
		{
			ccit->contactFromDNAndFolder( m_userId, m_displayName, m_firstSequenceNumber++, ( *it ).name );
		}
		else
			ccit->contactFromDN( m_userId, m_displayName, (*it).id );

		ccit->go( true );
	}

	if ( m_topLevel )
	{
		client()->debug( " - contact is in top level folder " );
		CreateContactInstanceTask * ccit = new CreateContactInstanceTask( client()->rootTask() );
		connect( ccit, TQT_SIGNAL( gotContactAdded( const ContactItem & ) ), TQT_SLOT( slotContactAdded( const ContactItem & ) ) );
        connect( ccit, TQT_SIGNAL( finished() ), TQT_SLOT( slotCheckContactInstanceCreated() ) );
		ccit->contactFromDN( m_userId, m_displayName, 0 );
		ccit->go( true );
	}
	client()->debug( "CreateContactTask::onGo() - DONE" );
}

void CreateContactTask::slotContactAdded( const ContactItem & addedContact )
{
	client()->debug( "CreateContactTask::slotContactAdded()" );
	// as each contact instance has been added on the server, 
	// remove the folderitem it belongs in.
	// once the list is empty, we have been successful

	if ( addedContact.displayName != m_displayName )
	{
		client()->debug( " - addedContact is not the one we were trying to add, ignoring it ( Account will update it )" );
		return;
	}
	client()->debug( TQString( "CreateContactTask::slotContactAdded() - Contact Instance %1 was created on the server, with objectId %2 in folder %3" ).tqarg
			( addedContact.displayName ).tqarg( addedContact.id ).tqarg( addedContact.parentId ) );
			
	if ( m_dn.isEmpty() )
		m_dn = addedContact.dn;
			
			
	if ( !m_folders.isEmpty() )
		m_folders.pop_back();

	// clear the topLevel flag once the corresponding server side entry has been successfully created
	if ( addedContact.parentId == 0 )
		m_topLevel = false;
	
	if ( m_folders.isEmpty() && !m_topLevel )
	{
		client()->debug( "CreateContactTask::slotContactAdded() - All contacts were created on the server, we're finished!" );
		setSuccess(); 
	}
}
void CreateContactTask::slotCheckContactInstanceCreated()
{
	CreateContactInstanceTask * ccit = ( CreateContactInstanceTask * )sender();
	if ( !ccit->success() )
	{
		setError( ccit->statusCode(), ccit->statusString() );
	}
}

#include "createcontacttask.moc"