📄 iksdemoui.cpp
字号:
/*
** Copyright (C) 2005 Darrell Karbott (djk2005@users.sf.net)
** This code is free software; you can redistribute it and/or modify
** it under the terms of the GNU Public Licence (GPL) version 2 See
** http://www.gnu.org/ for further details of the GPL.
*/
#include "iksdemoui.h"
#include "iksdemoui.hrh"
#include "symbian-ikstransport.h"
#include <ciksdemo.rsg>
#include <eikseced.h>
#include <eikcmbox.h>
#include <eikedwin.h>
#include <eikdialg.h>
#include <gulfont.h>
#include <reent.h>
////////////////////////////////////////////////////////////
CIksDemoDocument::CIksDemoDocument(CEikApplication& aApp)
: CQikDocument(aApp)
{
}
CIksDemoDocument::~CIksDemoDocument() {}
////////////////////////////////////////////////////////////
void CIksDemoAppUi::ConstructL()
{
iLoginJids = new (ELeave) CDesCArraySeg(4);
iLoginJids->AppendL(_L("@jabber.org"));
iSendJids = new (ELeave) CDesCArraySeg(4);
BaseConstructL();
iView = new (ELeave) CIksDemoAppView;
iView->ConstructL(ClientRect());
// Create an instance of the demo application and
// connect it to the UIQ UI.
iDemo = new CIksDemo(iks_symbian_ikstransport());
User::LeaveIfNull(iDemo);
iUiAdapter = new (ELeave) CIksDemoUserInterfaceAdapter(iView);
if (!iDemo->init(iUiAdapter)) { User::Leave(KErrNoMemory); }
iInteractionAdapter = new (ELeave) CIksDemoUserInteractionsAdapter(iDemo->cmd());
}
CIksDemoAppUi::~CIksDemoAppUi()
{
if (iDemo) {
// This keeps iDemo from calling back into iUiAdapter if it needs to
// disconnect in the destructor.
iDemo->releaseUi();
}
delete iDemo;
delete iUiAdapter;
delete iInteractionAdapter;
delete iView;
delete iSendJids;
delete iLoginJids;
// You must do this or the emulator will report
// a memory leak.
CloseSTDLIB();
}
MIksDemoUserInteractions* CIksDemoAppUi::Cmd()
{
return iInteractionAdapter;
}
////////////////////////////////////////////////////////////
class CConnectDialog : public CEikDialog {
public:
// Selected jid is at iJids[0]
CConnectDialog(CDesCArray& jids, TDes& password);
protected:
virtual TBool OkToExitL(TInt aKeyCode);
virtual void PreLayoutDynInitL();
private:
CDesCArray& iJids;
TDes& iPassword;
};
CConnectDialog::CConnectDialog(CDesCArray& jids, TDes& password) : iJids(jids), iPassword(password) {}
void SelectL(CDesCArray& list, const TDesC& selection)
{
TInt pos;
TInt found = list.Find(selection, pos);
if (found == 0) { list.Delete(pos); }
// Bound length
const TInt MAX_LIST_LEN = 9;
if (list.Count() > MAX_LIST_LEN) {
list.Delete(list.Count() - 1);
}
list.InsertL(0, selection);
return;
}
TBool CConnectDialog::OkToExitL(TInt /*aKeyCode*/)
{
TBuf<MAX_JID_LEN> jid;
CEikComboBox *edbox = STATIC_CAST(CEikComboBox*, Control(ECIksDemoJid));
edbox->GetText(jid);
if (jid.Length() == 0) { return EFalse; }
TBuf<64> password;
CEikSecretEditor *sedbox = STATIC_CAST(CEikSecretEditor*, Control(ECIksDemoPassword));
sedbox->GetText(password);
if (password.Length() == 0) { return EFalse; }
SelectL(iJids, jid);
iPassword = password;
return ETrue;
}
void CConnectDialog::PreLayoutDynInitL()
{
CEikComboBox *edbox = STATIC_CAST(CEikComboBox*, Control(ECIksDemoJid));
edbox->SetArray(&iJids, CEikComboBox::EArrayExternalOwner);
if (iJids.Count() > 0) {
const TDesC& latest = iJids[0];
edbox->SetTextL(&latest);
}
CEikSecretEditor *sedbox = STATIC_CAST(CEikSecretEditor*, Control(ECIksDemoPassword));
sedbox->SetText(iPassword);
}
////////////////////////////////////////////////////////////
void CIksDemoAppUi::UpdateSendJidsL(const TDesC& latest)
{
SelectL(*iSendJids, latest);
}
void CIksDemoAppUi::HandleConnectL()
{
CEikDialog* dialog = new (ELeave) CConnectDialog(*iLoginJids, iPassword);
if (dialog->ExecuteLD(R_CONNECT_DIALOG)) {
iView->DisplayJabberMsgFrom(_L(""), _L("")); // Hack clears starting message.
Cmd()->Connect((*iLoginJids)[0], iPassword, 5222);
}
}
////////////////////////////////////////////////////////////
// LATER: Reduce c&p by factoring shared dialog code
// into a common base class.
class CSendDialog : public CEikDialog {
public:
// Selected jid is at iJids[0]
CSendDialog(CDesCArray& jids, TDes& msg);
protected:
virtual TBool OkToExitL(TInt aKeyCode);
virtual void PreLayoutDynInitL();
private:
CDesCArray& iJids;
TDes& iMsg;
};
CSendDialog::CSendDialog(CDesCArray& jids, TDes& msg) : iJids(jids), iMsg(msg) {}
TBool CSendDialog::OkToExitL(TInt /*aKeyCode*/)
{
TBuf<MAX_JID_LEN> jid;
CEikComboBox *edbox = STATIC_CAST(CEikComboBox*, Control(ECIksDemoJid));
edbox->GetText(jid);
if (jid.Length() == 0) { return EFalse; }
TBuf<MAX_MSG_LEN> msg;
CEikEdwin *edwin = STATIC_CAST(CEikEdwin*, Control(ECIksDemoMsg));
edwin->GetText(msg);
if (msg.Length() == 0) { return EFalse; }
SelectL(iJids, jid);
iMsg = msg;
return ETrue;
}
void CSendDialog::PreLayoutDynInitL()
{
CEikComboBox *edbox = STATIC_CAST(CEikComboBox*, Control(ECIksDemoJid));
edbox->SetArray(&iJids, CEikComboBox::EArrayExternalOwner);
if (iJids.Count() > 0) {
const TDesC& latest = iJids[0];
edbox->SetTextL(&latest);
}
}
////////////////////////////////////////////////////////////
void CIksDemoAppUi::HandleSendMsgL()
{
TBuf<MAX_MSG_LEN> msg;
CEikDialog* dialog = new (ELeave) CSendDialog(*iSendJids, msg);
if (dialog->ExecuteLD(R_SEND_DIALOG)) {
Cmd()->SendMsg((*iSendJids)[0], msg);
}
}
void CIksDemoAppUi::HandleCommandL(TInt aCommand)
{
switch (aCommand)
{
case ECmdConnect:
HandleConnectL();
break;
case ECmdDisconnect:
Cmd()->Disconnect();
break;
case ECmdSendMsg:
HandleSendMsgL();
break;
case EEikCmdExit:
Exit();
break;
}
}
////////////////////////////////////////////////////////////
CIksDemoAppView::~CIksDemoAppView()
{
}
void CIksDemoAppView::ConstructL(const TRect& aRect)
{
iZoom.SetGraphicsDeviceMap(CCoeEnv::Static()->ScreenDevice());
CreateWindowL();
SetRect(aRect);
iStatusMsg = _L("Never Connected");
iMsgJid = _L("");
iMsg = _L("Use the Connect menu option\nto connect!");
ActivateL();
}
void CIksDemoAppView::DisplayJabberMsgFrom(const TDesC& from, const TDesC& msg )
{
iMsgJid = from;
iMsg = msg;
// Make it easy to reply.
CIksDemoAppUi* app = STATIC_CAST(CIksDemoAppUi*, CEikonEnv::Static()->EikAppUi());
TRAPD(ignoreError, app->UpdateSendJidsL(from));
DrawNow();
}
void CIksDemoAppView::DisplayStatusMsg(const TDesC& msg)
{
iStatusMsg = msg;
DrawNow();
}
void CIksDemoAppView::DisplayErrorMsg(const TDesC& msg)
{
CEikonEnv::Static()->AlertWin(_L("CIksDemo Error"), msg);
}
void CIksDemoAppView::UpdateConnectionStatus(TBool) { /*hmmm...*/}
void CIksDemoAppView::Draw(const TRect& /*aRect*/) const
{
const TInt YOFFSET = 20;
CWindowGc& gc = SystemGc();
gc.Clear();
TBuf<MAX_MSG_LEN + 20> status;
status.Append(_L("STATUS: "));
status.Append(iStatusMsg);
TInt descent0 = DrawWrappedTextL(status, YOFFSET, gc);
TInt descent1 = DrawWrappedTextL(_L("---"), YOFFSET + descent0, gc);
TInt descent2 = DrawWrappedTextL(iMsgJid, YOFFSET + descent0 + descent1, gc);
DrawWrappedTextL(iMsg, YOFFSET + descent0 + descent1 + descent2, gc);
}
TInt CIksDemoAppView::DrawWrappedTextL(const TDesC& text, TInt y, CWindowGc& gc) const
{
const TInt XOFFSET=5;
// Set font
TLogicalFont lf(TLogicalFont::EView, TLogicalFont::ENormal, iZoom);
const CFont* fontUsed = CEikonEnv::Static()->Font(lf);
gc.UseFont(fontUsed);
TInt height = fontUsed->HeightInPixels() + 2 /*hack*/;
TInt offset = y;
// KMart line breaking is ok for demo code, but not really up to snuff.
TPtrC cursor = text;
while (cursor.Length() > 0) {
TInt charsDrawn = fontUsed->TextCount(cursor, Size().iWidth - 2*XOFFSET);
TPtrC drawn = cursor.Left(charsDrawn);
if (drawn.Find(_L("\n")) != KErrNotFound) {
drawn.Set(drawn.Left(drawn.Find(_L("\n")) + 1));
}
cursor.Set(cursor.Right(cursor.Length() - drawn.Length()));
if (drawn.Find(_L("\n")) != KErrNotFound) {
drawn.Set(drawn.Left(drawn.Find(_L("\n"))));
}
gc.DrawText(drawn, TPoint(XOFFSET, offset));
offset += height;
}
// Release font.
gc.DiscardFont();
return offset - y;
}
////////////////////////////////////////////////////////////
CApaDocument* CIksDemoApplication::CreateDocumentL()
{
return new (ELeave) CIksDemoDocument(*this);
}
TUid CIksDemoApplication::AppDllUid() const
{
return KUidIksDemoApp;
}
////////////////////////////////////////////////////////////
CEikAppUi* CIksDemoDocument::CreateAppUiL()
{
return new (ELeave) CIksDemoAppUi;
}
////////////////////////////////////////////////////////////
EXPORT_C CApaApplication* NewApplication()
{
return new CIksDemoApplication;
}
GLDEF_C TInt E32Dll(TDllReason)
{
return KErrNone;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -