summaryrefslogtreecommitdiffstats
path: root/tderesources/scalix/scalixadmin/otheruserpage.cpp
blob: 3aac47c9e6d56d6ec4d44eb17d5fae0551138855 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 *   This file is part of ScalixAdmin.
 *
 *   Copyright (C) 2007 Trolltech ASA. All rights reserved.
 *
 *   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.
 *
 *   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.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <tqapplication.h>
#include <tqlayout.h>
#include <tqpushbutton.h>

#include <dcopref.h>
#include <kdcopservicestarter.h>
#include <kinputdialog.h>
#include <klocale.h>
#include <kmessagebox.h>

#include <unistd.h>

#include "jobs.h"
#include "ldapdialog.h"
#include "otheruserview.h"
#include "settings.h"

#include "otheruserpage.h"

OtherUserPage::OtherUserPage( TQWidget *parent )
  : TQWidget( parent )
{
  TQGridLayout *layout = new TQGridLayout( this, 2, 2, 11, 6 );

  mView = new OtherUserView( &mManager, this );
  layout->addMultiCellWidget( mView, 0, 0, 0, 1 );

  mAddButton = new TQPushButton( i18n( "Add Account..." ), this );
  layout->addWidget( mAddButton, 1, 0 );

  mDeleteButton = new TQPushButton( i18n( "Remove Account" ), this );
  mDeleteButton->setEnabled( false );
  layout->addWidget( mDeleteButton, 1, 1 );

  connect( mView, TQT_SIGNAL( selectionChanged() ), TQT_SLOT( selectionChanged() ) );
  connect( mAddButton, TQT_SIGNAL( clicked() ), TQT_SLOT( addUser() ) );
  connect( mDeleteButton, TQT_SIGNAL( clicked() ), TQT_SLOT( removeUser() ) );

  loadAllUsers();
}

OtherUserPage::~OtherUserPage()
{
}

void OtherUserPage::loadAllUsers()
{
  Scalix::GetOtherUsersJob *job = Scalix::getOtherUsers( Settings::self()->globalSlave(),
                                                         Settings::self()->accountUrl() );
  connect( job, TQT_SIGNAL( result( TDEIO::Job* ) ), TQT_SLOT( allUsers( TDEIO::Job* ) ) );
}

void OtherUserPage::addUser()
{
  LdapDialog dlg( this );
  if ( !dlg.exec() )
    return;

  const TQString email = dlg.selectedUser();
  if ( email.isEmpty() )
    return;

  Scalix::AddOtherUserJob *job = Scalix::addOtherUser( Settings::self()->globalSlave(),
                                                       Settings::self()->accountUrl(), email );
  connect( job, TQT_SIGNAL( result( TDEIO::Job* ) ), TQT_SLOT( userAdded( TDEIO::Job* ) ) );
}

void OtherUserPage::removeUser()
{
  const TQString email = mView->selectedUser();
  if ( email.isEmpty() )
    return;

  Scalix::DeleteOtherUserJob *job = Scalix::deleteOtherUser( Settings::self()->globalSlave(),
                                                             Settings::self()->accountUrl(), email );
  connect( job, TQT_SIGNAL( result( TDEIO::Job* ) ), TQT_SLOT( userRemoved( TDEIO::Job* ) ) );
}

void OtherUserPage::allUsers( TDEIO::Job *job )
{
  if ( job->error() )
    KMessageBox::error( this, job->errorString() );

  Scalix::GetOtherUsersJob *userJob = static_cast<Scalix::GetOtherUsersJob*>( job );

  mManager.clear();

  const TQStringList users = userJob->otherUsers();
  for ( uint i = 0; i < users.count(); ++i )
    mManager.addOtherUser( users[ i ] );

  selectionChanged();
}

void OtherUserPage::userAdded( TDEIO::Job *job )
{
  if ( job->error() )
    KMessageBox::error( this, job->errorString() );
  else
    loadAllUsers(); // update the GUI

  updateKmail();
}

void OtherUserPage::userRemoved( TDEIO::Job *job )
{
  if ( job->error() )
    KMessageBox::error( this, job->errorString() );
  else
    loadAllUsers(); // update the GUI

  updateKmail();
}

void OtherUserPage::selectionChanged()
{
  mDeleteButton->setEnabled( mView->selectedItem() != 0 );
}

void OtherUserPage::updateKmail()
{
  TQMessageBox *msg = new TQMessageBox( tqApp->mainWidget() );
  msg->setText( i18n( "Updating account..." ) );
  msg->show();
  tqApp->processEvents();
  sleep( 1 );
  tqApp->processEvents();

  TQString error;
  TQCString dcopService;
  int result = KDCOPServiceStarter::self()->
    findServiceFor( "DCOP/ResourceBackend/IMAP", TQString(),
                    TQString(), &error, &dcopService );
  if ( result != 0 ) {
    KMessageBox::error( 0, i18n( "Unable to start KMail to trigger account update with Scalix server" ) );
    delete msg;
    return;
  }

  DCOPRef ref( dcopService, "KMailIface" );

  // loop until dcop iface is set up correctly
  TQStringList list;
  while ( list.isEmpty() ) {
    ref.call( "accounts()" ).get( list );
  }

  ref.call( "checkAccount(TQString)", i18n( "Scalix Server" ) );

  delete msg;
}

#include "otheruserpage.moc"