Branch libreoffice-5-0-4
[LibreOffice.git] / tubes / source / contacts.cxx
blobdf9900565dd871235bf1f7c49b89caa56a0e2de0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <sal/config.h>
12 #include <vcl/graphicfilter.hxx>
13 #include <tubes/conference.hxx>
14 #include <tubes/collaboration.hxx>
15 #include <tubes/manager.hxx>
16 #include <vcl/button.hxx>
17 #include <vcl/dialog.hxx>
18 #include <vcl/lstbox.hxx>
20 #include <telepathy-glib/telepathy-glib.h>
22 namespace {
24 class TubeContacts : public ModelessDialog
26 PushButton* mpBtnDemo;
27 PushButton* mpBtnBuddy;
28 PushButton* mpBtnGroup;
29 PushButton* mpBtnInvite;
30 PushButton* mpBtnListen;
31 ListBox* mpList;
32 Collaboration* mpCollaboration;
34 DECL_LINK( BtnDemoHdl, void * );
35 DECL_LINK( BtnConnectHdl, void * );
36 DECL_LINK( BtnGroupHdl, void * );
37 DECL_LINK( BtnInviteHdl, void * );
38 DECL_LINK( BtnListenHdl, void * );
40 AccountContactPairV maACs;
42 void Invite()
44 if (mpList->GetSelectEntryCount())
46 sal_uInt16 i = mpList->GetSelectEntryPos();
47 TpContact* pContact = maACs[i].second;
48 mpCollaboration->Invite( pContact );
52 void Listen()
54 if (!TeleManager::registerClients())
55 SAL_INFO( "tubes", "Could not register client handlers." );
58 void StartDemoSession()
60 TeleConference* pConference = TeleManager::startDemoSession();
61 if (!pConference)
62 SAL_WARN( "tubes", "Could not start demo session!" );
63 else
65 mpCollaboration->StartCollaboration( pConference );
66 mpCollaboration->SaveAndSendFile( NULL );
70 void StartBuddySession()
72 if (mpList->GetSelectEntryCount())
74 sal_uInt16 i = mpList->GetSelectEntryPos();
75 TpAccount* pAccount = maACs[i].first;
76 TpContact* pContact = maACs[i].second;
77 SAL_INFO( "tubes", "picked " << tp_contact_get_identifier( pContact ) );
78 TeleConference* pConference = TeleManager::startBuddySession( pAccount, pContact );
79 if (!pConference)
80 SAL_WARN( "tubes", "Could not start session with " <<
81 tp_contact_get_identifier( pContact ) );
82 else
84 mpCollaboration->StartCollaboration( pConference );
85 mpCollaboration->SaveAndSendFile( pContact );
90 void StartGroupSession()
92 if (mpList->GetSelectEntryCount())
94 sal_uInt16 i = mpList->GetSelectEntryPos();
95 TpAccount* pAccount = maACs[i].first;
96 SAL_INFO( "tubes", "picked " << tp_account_get_display_name( pAccount ) );
97 TeleConference* pConference = TeleManager::startGroupSession( pAccount,
98 OUString("liboroom"), OUString("conference.jabber.org") );
99 if (!pConference)
100 SAL_WARN( "tubes", "Could not start group session." );
101 else
103 mpCollaboration->StartCollaboration( pConference );
108 public:
109 TubeContacts( Collaboration* pCollaboration ) :
110 ModelessDialog( NULL, "ContactsDialog", "tubes/ui/contacts.ui" ),
111 mpCollaboration( pCollaboration )
113 get( mpBtnListen, "listen");
114 get( mpBtnInvite, "invite");
115 get( mpBtnDemo, "demo");
116 get( mpBtnBuddy, "buddy");
117 get( mpBtnGroup, "group");
118 get( mpList, "contacts");
119 mpBtnListen->SetClickHdl( LINK( this, TubeContacts, BtnListenHdl ) );
120 mpBtnInvite->SetClickHdl( LINK( this, TubeContacts, BtnInviteHdl ) );
121 mpBtnDemo->SetClickHdl( LINK( this, TubeContacts, BtnDemoHdl ) );
122 mpBtnBuddy->SetClickHdl( LINK( this, TubeContacts, BtnConnectHdl ) );
123 mpBtnGroup->SetClickHdl( LINK( this, TubeContacts, BtnGroupHdl ) );
125 virtual ~TubeContacts()
129 static OUString fromUTF8( const char *pStr )
131 return OStringToOUString( OString( pStr, strlen( pStr ) ),
132 RTL_TEXTENCODING_UTF8 );
135 void Populate()
137 SAL_INFO( "tubes", "Populating contact list dialog" );
138 mpList->Clear();
139 maACs.clear();
141 AccountContactPairV aPairs = TeleManager::getContacts();
142 AccountContactPairV::iterator it;
143 // make sure we have enough memory to not need re-allocation
144 // which would invalidate pointers stored in mpList entries
145 maACs.reserve( aPairs.size() );
146 for( it = aPairs.begin(); it != aPairs.end(); ++it )
148 Image aImage;
149 GFile *pAvatarFile = tp_contact_get_avatar_file( it->second );
150 if( pAvatarFile )
152 const OUString sAvatarFileUrl = fromUTF8( g_file_get_path ( pAvatarFile ) );
153 Graphic aGraphic;
154 if( GRFILTER_OK == GraphicFilter::LoadGraphic( sAvatarFileUrl, OUString(""), aGraphic ) )
156 BitmapEx aBitmap = aGraphic.GetBitmapEx();
157 double fScale = 30.0 / aBitmap.GetSizePixel().Height();
158 aBitmap.Scale( fScale, fScale );
159 aImage = Image( aBitmap );
162 OUStringBuffer aEntry( 128 );
163 aEntry.append( " " );
164 aEntry.append( fromUTF8 ( tp_contact_get_alias( it->second ) ) );
165 aEntry.append( " - " );
166 aEntry.append( fromUTF8 ( tp_contact_get_identifier( it->second ) ) );
167 mpList->InsertEntry( aEntry.makeStringAndClear(), aImage);
168 // FIXME: ref the TpAccount, TpContact ...
169 maACs.push_back( AccountContactPair( it->first, it->second ) );
171 g_object_unref (it->first);
172 g_object_unref (it->second);
174 Show();
178 IMPL_LINK_NOARG( TubeContacts, BtnDemoHdl )
180 StartDemoSession();
181 return 0;
184 IMPL_LINK_NOARG( TubeContacts, BtnConnectHdl )
186 StartBuddySession();
187 return 0;
190 IMPL_LINK_NOARG( TubeContacts, BtnGroupHdl )
192 StartGroupSession();
193 return 0;
196 IMPL_LINK_NOARG( TubeContacts, BtnInviteHdl )
198 Invite();
199 return 0;
202 IMPL_LINK_NOARG( TubeContacts, BtnListenHdl )
204 Listen();
205 return 0;
208 } // anonymous namespace
210 void Collaboration::DisplayContacts()
212 if (!mpContacts)
213 mpContacts = new TubeContacts( this );
214 reinterpret_cast<TubeContacts*> (mpContacts)->Populate();
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */