messagingapp/msgui/conversationview/src/msgeditorwidget.cpp
branchRCL_3
changeset 57 ebe688cedc25
equal deleted inserted replaced
54:fa1df4b99609 57:ebe688cedc25
       
     1 /*
       
     2  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description:
       
    15  *
       
    16  */
       
    17 
       
    18 #include "msgeditorwidget.h"
       
    19 
       
    20 // SYSTEM INCLUDES
       
    21 #include <HbStyle>
       
    22 #include <HbPushButton>
       
    23 #include <HbTextItem>
       
    24 #include <qgraphicsscene.h>
       
    25 #include <HbFrameDrawer>
       
    26 #include <HbFrameItem>
       
    27 #include <HbAction>
       
    28 #include <csmsaccount.h>
       
    29 #include <smutset.h>
       
    30 #include <hbmessagebox.h>
       
    31 #include <hbcolorscheme.h>
       
    32 #include <QColor>
       
    33 #include <QTimer>
       
    34 #include "unieditorpluginloader.h"
       
    35 #include "unieditorplugininterface.h"
       
    36 #include "UniSendingSettings.h"
       
    37 #include "UniEditorGenUtils.h"
       
    38 #include "debugtraces.h"
       
    39 
       
    40 // LOCAL CONSTANTS
       
    41 
       
    42 const QString SEND_ICON("qtg_mono_send");
       
    43 const QString BACKGROUND("qtg_fr_input_v_bg");
       
    44 const QString BACKGROUND_FRAME("qtg_fr_messaging_char_count");
       
    45 const QString CHAR_COUNTER_COLOR("qtc_messaging_char_count");
       
    46 
       
    47 const QString SEND_BUTTON_NORMAL("qtg_fr_btn_green_normal");
       
    48 const QString SEND_BUTTON_PRESSED("qtg_fr_btn_green_pressed");
       
    49 const QString SEND_BUTTON_DISABLED("qtg_fr_btn_disabled");
       
    50 
       
    51 const QString SEND_BUTTON_NORMAL_COLOR("qtc_callhandling_answer_normal");
       
    52 const QString SEND_BUTTON_PRESSED_COLOR("qtc_callhandling_answer_pressed");
       
    53 const QString SEND_BUTTON_DISABLED_COLOR("qtc_button_disabled");
       
    54 
       
    55 #define LOC_SMS_CHAR_LIMIT_REACHED hbTrId("txt_messaging_dialog_sms_character_count_exceeded")
       
    56 #define LOC_HINT_TEXT hbTrId("txt_messaging_formlabel_enter_message_here")
       
    57 
       
    58   
       
    59 const TInt KShowCounterLimit = 10;
       
    60 
       
    61 QGraphicsItem* FOCUSITEM = 0;
       
    62 
       
    63 //---------------------------------------------------------------
       
    64 // MsgEditorWidget::MsgEditorWidget
       
    65 // @see header
       
    66 //---------------------------------------------------------------
       
    67 MsgEditorWidget::MsgEditorWidget(QGraphicsItem *parent) :
       
    68     HbWidget(parent), mMsgEditor(NULL),
       
    69     mSendButton(NULL),mPluginLoader(NULL)
       
    70 {
       
    71     //setting background.
       
    72     HbFrameItem* backGround = new HbFrameItem(this);
       
    73     backGround->frameDrawer().setFrameGraphicsName(BACKGROUND);
       
    74     backGround->frameDrawer().setFrameType(HbFrameDrawer::ThreePiecesVertical);
       
    75     backGround->frameDrawer().setFillWholeRect(true);
       
    76     this->setBackgroundItem(backGround); 
       
    77 
       
    78     // Initialize the widget.
       
    79     init();
       
    80 }
       
    81 
       
    82 //---------------------------------------------------------------
       
    83 // MsgEditorWidget::init
       
    84 // @see header
       
    85 //---------------------------------------------------------------
       
    86 void MsgEditorWidget::init()
       
    87 {
       
    88     // Create mandatory element of mesh layout.
       
    89     mMsgEditor = new MsgEditor(this);
       
    90     mMsgEditor->setMaxRows(3); // NOTE: Don't remove this line.
       
    91     HbStyle::setItemName(mMsgEditor, "msgEditor");
       
    92 
       
    93 	mSendButton = new HbPushButton(this); 
       
    94     HbStyle::setItemName(mSendButton, "sendButton"); 
       
    95     mSendButton->setEnabled(false); 
       
    96     
       
    97     HbFrameItem* backGround = new HbFrameItem(this); 
       
    98     backGround->frameDrawer().setFrameType(HbFrameDrawer::NinePieces); 
       
    99     mSendButton->setBackgroundItem(backGround); 
       
   100     updateButtonBackgroundAndColor(SEND_BUTTON_DISABLED,SEND_BUTTON_DISABLED_COLOR); 
       
   101     mSendButton->setIcon(HbIcon(SEND_ICON));
       
   102     
       
   103     mCharCounter = new HbTextItem(this);
       
   104     HbStyle::setItemName(mCharCounter, "charCounter");
       
   105     mCharCounter->setZValue(1.5);
       
   106     
       
   107     QColor color = HbColorScheme::color(CHAR_COUNTER_COLOR);
       
   108     mCharCounter->setTextColor(color);
       
   109     
       
   110     mBackgroundItem = new HbFrameItem(this);
       
   111     HbStyle::setItemName(mBackgroundItem, "charCounterFrame");
       
   112 
       
   113     mBackgroundItem->frameDrawer().setFrameType(HbFrameDrawer::ThreePiecesHorizontal);
       
   114     mBackgroundItem->frameDrawer().setFillWholeRect(true);
       
   115     
       
   116     mBackgroundItem->frameDrawer().setFrameGraphicsName(
       
   117         BACKGROUND_FRAME);    
       
   118     
       
   119     //Create editor utils object
       
   120     mEditorUtils = q_check_ptr(new UniEditorGenUtils());
       
   121 
       
   122     FOCUSITEM = mSendButton;
       
   123 
       
   124 
       
   125     connect(mSendButton, SIGNAL(clicked()),this, SLOT(onClicked()));
       
   126     connect(mSendButton, SIGNAL(pressed()),this, SLOT(onPressed()));
       
   127     connect(mSendButton, SIGNAL(released()),this, SLOT(onReleased()));
       
   128     connect(mMsgEditor, SIGNAL(replyStarted()),this, SIGNAL(replyStarted()));
       
   129     connect(mMsgEditor, SIGNAL(textChanged(QString)),this,SLOT(onTextChanged(QString)));
       
   130     
       
   131 }
       
   132 
       
   133 //---------------------------------------------------------------
       
   134 // MsgEditorWidget::~MsgEditorWidget
       
   135 // @see header
       
   136 //---------------------------------------------------------------
       
   137 MsgEditorWidget::~MsgEditorWidget()
       
   138 {
       
   139     delete mEditorUtils;
       
   140 }
       
   141 
       
   142 //---------------------------------------------------------------
       
   143 // MsgEditorWidget::content
       
   144 // @see header
       
   145 //---------------------------------------------------------------
       
   146 QString MsgEditorWidget::content() const
       
   147 {
       
   148     return mMsgEditor->text();
       
   149 }
       
   150 
       
   151 //---------------------------------------------------------------
       
   152 // MsgEditorWidget::setContent
       
   153 // @see header
       
   154 //---------------------------------------------------------------
       
   155 void MsgEditorWidget::setContent(const QString &text)
       
   156 {
       
   157     mMsgEditor->setText(text);
       
   158 }
       
   159 
       
   160 //---------------------------------------------------------------
       
   161 // MsgEditorWidget::clear
       
   162 // @see header
       
   163 //---------------------------------------------------------------
       
   164 void MsgEditorWidget::clear()
       
   165 {
       
   166     mMsgEditor->setText(QString());
       
   167     mMsgEditor->setCursorVisibility(Hb::TextCursorHidden);
       
   168 }
       
   169 
       
   170 //---------------------------------------------------------------
       
   171 // MsgEditorWidget::onTextChanged
       
   172 // @see header
       
   173 //---------------------------------------------------------------
       
   174 void MsgEditorWidget::onTextChanged(const QString& str)
       
   175 {
       
   176     //If char limit is already reached we are about to show the sms limit dialog
       
   177     //So we need not process the additional character entered
       
   178     if(mSmsCharLimitReached)
       
   179     {
       
   180         return;
       
   181     }
       
   182     if(str.length() > 0 )
       
   183         {
       
   184         if(!mSendButton->isEnabled())
       
   185             {
       
   186             mSendButton->setFocusProxy(mMsgEditor);
       
   187             mSendButton->setEnabled(true);
       
   188             updateButtonBackgroundAndColor(SEND_BUTTON_NORMAL,SEND_BUTTON_NORMAL_COLOR);
       
   189             }
       
   190         }
       
   191     else
       
   192         {
       
   193         if(mSendButton->isEnabled())
       
   194             {
       
   195             mSendButton->setFocusProxy(0);
       
   196             mSendButton->setEnabled(false);
       
   197             updateButtonBackgroundAndColor(SEND_BUTTON_DISABLED,SEND_BUTTON_DISABLED_COLOR);
       
   198             }
       
   199         }
       
   200     
       
   201     //Check done for optimization
       
   202     //Only if content is deleted we need to call encodingsettings again
       
   203     if(mPrevBuffer.isEmpty() || str.size() <= mPrevBuffer.size())
       
   204     {
       
   205     mPluginInterface->setEncodingSettings(EFalse,ESmsEncodingNone,mCharSupportType);
       
   206     }
       
   207     
       
   208     TInt numOfRemainingChars;
       
   209     TInt numOfPDUs;
       
   210     TBool unicodeMode;
       
   211     TSmsEncoding alternativeEncodingType;
       
   212     QString string = str;
       
   213     mPluginInterface->getNumPDUs(string, numOfRemainingChars, numOfPDUs,
       
   214         unicodeMode, alternativeEncodingType);
       
   215 
       
   216     //Set char counter value
       
   217     QString display = QString("%1(%2)").arg(numOfRemainingChars).arg(numOfPDUs);
       
   218     mCharCounter->setText(display);
       
   219     
       
   220     if(numOfPDUs > 1 || numOfRemainingChars <= KShowCounterLimit  )
       
   221     {
       
   222         mCharCounter->setVisible(true);
       
   223         mBackgroundItem->setVisible(true);
       
   224     }
       
   225     else
       
   226     {
       
   227         mCharCounter->setVisible(false);
       
   228         mBackgroundItem->setVisible(false);
       
   229     }
       
   230     
       
   231     //Check if sms segment limit has been reached
       
   232     bool unicode = (unicodeMode) ? true : false;
       
   233     int contentSize = mEditorUtils->UTF8Size(string);
       
   234     int maxSmsSize =  0;
       
   235     TRAP_IGNORE(maxSmsSize = mEditorUtils->MaxSmsMsgSizeL(unicode));
       
   236     
       
   237     if(contentSize > maxSmsSize)
       
   238     {        
       
   239         //Give breathing time for ITUT to complete its processing
       
   240         //without this there is a crash as ITUT cant complete its processing
       
   241         mSmsCharLimitReached = true;
       
   242         QTimer::singleShot(50, this, SLOT(handleSmsCharLimitReached()));   
       
   243         if(mPrevBuffer.isEmpty())
       
   244         {
       
   245             //Chop the last char and set it to the editor
       
   246             string.chop(1);
       
   247             mPrevBuffer = string;
       
   248         }
       
   249     }
       
   250     else
       
   251     {                
       
   252         //Save the previous buffer
       
   253         mPrevBuffer = string;        
       
   254     }
       
   255 }
       
   256 
       
   257 //---------------------------------------------------------------
       
   258 // MsgEditorWidget::handleSmsCharLimitReached
       
   259 // @see header
       
   260 //---------------------------------------------------------------
       
   261 void MsgEditorWidget::handleSmsCharLimitReached()
       
   262 {
       
   263     mSmsCharLimitReached = false;
       
   264     
       
   265     HbMessageBox::question(LOC_SMS_CHAR_LIMIT_REACHED,
       
   266                            this,SLOT(onSmsCharLimitReached(HbAction*)),
       
   267                            HbMessageBox::Ok | HbMessageBox::Cancel);
       
   268 
       
   269 
       
   270 }
       
   271 
       
   272 //---------------------------------------------------------------
       
   273 // MsgEditor::onClicked
       
   274 // @see header
       
   275 //---------------------------------------------------------------
       
   276 void MsgEditorWidget::onClicked()
       
   277     {    
       
   278     mSendButton->setFocusProxy(0);
       
   279     mMsgEditor->setFocusProxy(mSendButton);
       
   280     
       
   281     this->scene()->clearFocus();
       
   282     this->scene()->setFocusItem(mSendButton);
       
   283     
       
   284     mMsgEditor->setFocusProxy(0);
       
   285 
       
   286     mMsgEditor->setCursorVisibility(Hb::TextCursorHidden);
       
   287     
       
   288     emit sendMessage();
       
   289     }
       
   290 
       
   291 //---------------------------------------------------------------
       
   292 // MsgEditor::updateButtonBackgroundAndColor
       
   293 // @see header
       
   294 //---------------------------------------------------------------
       
   295 void MsgEditorWidget::updateButtonBackgroundAndColor(const QString& bg,const QString& iconColor) 
       
   296     { 
       
   297     HbFrameItem* drawer = static_cast<HbFrameItem*>(mSendButton->backgroundItem()); 
       
   298     if(drawer) 
       
   299         { 
       
   300         drawer->frameDrawer().setFrameGraphicsName(bg);   
       
   301         } 
       
   302     QColor color = HbColorScheme::color(iconColor);
       
   303     
       
   304     if(color.isValid())
       
   305     	 {
       
   306     	 mSendButton->icon().setColor(color);
       
   307     	 }	    
       
   308     } 
       
   309     
       
   310 
       
   311 //---------------------------------------------------------------
       
   312 // MsgEditor::onPressed
       
   313 // @see header
       
   314 //---------------------------------------------------------------
       
   315 void MsgEditorWidget::onPressed()
       
   316     {
       
   317     updateButtonBackgroundAndColor(SEND_BUTTON_PRESSED,SEND_BUTTON_PRESSED_COLOR);
       
   318     }
       
   319 
       
   320 //---------------------------------------------------------------
       
   321 // MsgEditor::onReleased
       
   322 // @see header
       
   323 //---------------------------------------------------------------
       
   324 void MsgEditorWidget::onReleased()
       
   325     {
       
   326     updateButtonBackgroundAndColor(SEND_BUTTON_NORMAL,SEND_BUTTON_NORMAL_COLOR);
       
   327     }
       
   328 	
       
   329 //---------------------------------------------------------------
       
   330 // MsgEditor::setEncodingSettingsL
       
   331 // @see header
       
   332 //---------------------------------------------------------------
       
   333 void MsgEditorWidget::setEncodingSettingsL()
       
   334 { 
       
   335     if( mPluginLoader )
       
   336     {
       
   337         delete mPluginLoader;
       
   338     }
       
   339     mPluginLoader = new UniEditorPluginLoader(this);
       
   340 
       
   341     mPluginInterface =
       
   342                         mPluginLoader->getUniEditorPlugin(ConvergedMessage::Sms);    
       
   343 
       
   344     QT_TRAP_THROWING( CSmsSettings* settings = CSmsSettings::NewLC();
       
   345         CSmsAccount* account = CSmsAccount::NewLC();
       
   346         account->LoadSettingsL( *settings );
       
   347 
       
   348         if( settings->CharacterSet() == TSmsDataCodingScheme::ESmsAlphabetUCS2)
       
   349         {
       
   350             mCharSupportType = TUniSendingSettings::EUniCharSupportFull;
       
   351         }
       
   352         else
       
   353         {
       
   354             mCharSupportType = TUniSendingSettings::EUniCharSupportReduced;
       
   355         }
       
   356 
       
   357         CleanupStack::PopAndDestroy( account );
       
   358         CleanupStack::PopAndDestroy( settings ));
       
   359 
       
   360     //Set the mPrevBuffer to NULL initially
       
   361     mPrevBuffer = QString();
       
   362     
       
   363     mSmsCharLimitReached = false;
       
   364     
       
   365     mCharCounter->setVisible(false);
       
   366     mBackgroundItem->setVisible(false);
       
   367 }
       
   368 
       
   369 //---------------------------------------------------------------
       
   370 // MsgEditor::MsgEditor
       
   371 // @see header
       
   372 //---------------------------------------------------------------
       
   373 MsgEditor::MsgEditor(QGraphicsItem *parent)
       
   374 :HbLineEdit(parent)
       
   375     {
       
   376      this->setFontSpec(HbFontSpec(HbFontSpec::Secondary));
       
   377      this->setPlaceholderText(LOC_HINT_TEXT);
       
   378     }
       
   379 
       
   380 //---------------------------------------------------------------
       
   381 // MsgEditor::focusInEvent
       
   382 // @see header
       
   383 //---------------------------------------------------------------
       
   384 void MsgEditor::focusInEvent(QFocusEvent *event)
       
   385  {
       
   386     if(event->reason() == Qt::MouseFocusReason)
       
   387     {
       
   388         HbLineEdit::focusInEvent(event);
       
   389         FOCUSITEM->setFocusProxy(this);
       
   390         setCursorVisibility(Hb::TextCursorVisible);
       
   391         emit replyStarted();
       
   392     }
       
   393     else
       
   394     {
       
   395         setCursorVisibility(Hb::TextCursorHidden);
       
   396     }
       
   397  }
       
   398 
       
   399 //---------------------------------------------------------------
       
   400 // MsgEditor::focusOutEvent
       
   401 // @see header
       
   402 //---------------------------------------------------------------
       
   403 void MsgEditor::focusOutEvent(QFocusEvent * event)
       
   404     {
       
   405     FOCUSITEM->setFocusProxy(0);
       
   406     setCursorVisibility(Hb::TextCursorHidden);
       
   407     HbLineEdit::focusOutEvent(event);  
       
   408     }
       
   409 
       
   410 //---------------------------------------------------------------
       
   411 // MsgEditor::onSmsCharLimitReached
       
   412 // @see header
       
   413 //---------------------------------------------------------------
       
   414 void MsgEditorWidget::onSmsCharLimitReached(HbAction* action)
       
   415 {
       
   416     HbMessageBox *dlg = qobject_cast<HbMessageBox*> (sender());
       
   417     if (action == dlg->actions().at(0)) {
       
   418 
       
   419         //Launch UniEditor 
       
   420         emit smsCharLimitReached();
       
   421     }
       
   422     else {
       
   423         //Set the previous content
       
   424         setContent( QString(mPrevBuffer));
       
   425     }
       
   426 }
       
   427 
       
   428 // EOF