Wednesday, October 28, 2015

XMPP Chat - part one(Connection)

Here I am showing the steps to implement XMPP in to Blackberry 10 Application


  • Download QXMPP source code from here
  • Add required files in to the xmpp folder
  • Add QXmppClient *m_xmppClient; in application.h file
  • Add the following lines to application.cpp file
            m_xmppClient = new QXmppClient;
            QXmppConfiguration config;
            config.setAutoAcceptSubscriptions(true);
            config.setHost("hostname"); //ip added
            config.setDomain("domainname");//example.com
            config.setUser("username");
            config.setPassword("password");
            //config.setAutoReconnectionEnabled(true);
            m_xmppClient->connectToServer(config, QXmppPresence::Available);
            m_xmppClient->logger()->setLoggingType(QXmppLogger::StdoutLogging);
            
            //SIGNALS FOR XMPP CONNECTION
             connect(m_xmppClient, SIGNAL(connected()), this, SLOT(onChatConnected()));
             connect(m_xmppClient, SIGNAL(messageReceived(const QXmppMessage)), this,
                           SLOT(onMessageReceived(const QXmppMessage)));
  • void ApplicationUI::onMessageReceived(const QXmppMessage &message){
             qCritical()<<"Connected to XMPP"
          }




  • Will describe all the methods of XMPP in the next part.....


Tuesday, October 13, 2015

Wait Dialogue in Blackberry 10

Hi All,

  Here is the code for adding an activity indicator in Blackberry 10 like Android Progress Indicator.
This will disable all the components in the UI and show the indicator. This can be used for http requets

here is the code

Activity.qml page

import bb.cascades 1.0
Container {
    background: Color.Transparent
    
    layout: DockLayout {
    
    }
    
    minWidth: 720 // width of the screen
    minHeight: 1280 // height of the screen
    
    Container {
        minWidth: 650
        maxWidth: 650
        minHeight: 100
        maxHeight: 100
        background: Color.create("#a6c8e0")
        horizontalAlignment: HorizontalAlignment.Center
        verticalAlignment: VerticalAlignment.Center
        layout: DockLayout {
            
        }
        Container {
            background: Color.Gray
            minWidth: 646
            maxWidth: 646
            minHeight: 96
            maxHeight: 96
            horizontalAlignment: HorizontalAlignment.Center
            verticalAlignment: VerticalAlignment.Center
            leftPadding: 50
            layout: StackLayout {
                orientation: LayoutOrientation.LeftToRight
            }
            
            ActivityIndicator {
                minWidth: 80
                minHeight: 80
                objectName: "customActivityIndicator"
                horizontalAlignment: HorizontalAlignment.Center
                verticalAlignment: VerticalAlignment.Center
                running: true
            }
            Label {
                verticalAlignment: VerticalAlignment.Center
                text: qsTr("Loading please wait...")
                textStyle.fontSize: FontSize.Small
                textStyle.color: Color.create("#1F2E56")
            }
        }
       
    }
    attachedObjects:[ ImagePaintDefinition {
            id: receiveItem
            imageSource: "asset:///images/info_bg_common.amd"
    }]

}



Following is the method for adding add removing activity indicator in the screen


This is the CPP code

void DialogueUtils::addActivityDialogue()
{
    activityDialogue = new Dialog;
    QmlDocument *qmlacitivity = QmlDocument::create("asset:///Activity.qml");
    activityRootContainer = qmlacitivity->createRootObject<Container>();

    customActivityIndicator = activityRootContainer->findChild<ActivityIndicator*>(
            "customActivityIndicator");
    customActivityIndicator->start();
    activityDialogue->setContent(activityRootContainer);
    activityDialogue->open();
}


void DialogueUtils::removeActivityDialogue()
{
    customActivityIndicator->stop();
    activityDialogue->close();
}


To use this indicator in a page 
dialogueUtils-> addActivityDialogue();

To remove this 
dialogueUtils-> removeActivityDialogue();

Wednesday, March 27, 2013

Image Button

public class ImageButton extends Field {
    /* Implementation details of ImageButton :   
     */
   
    private Bitmap backgroundImage = null;
   


    private Bitmap pressedImage = null;
   
    private int width = 0;
   
    private boolean focusable = true;
   
    private boolean selected = false;
   
    /**
     * <p>This method can be used for getting the selected.</p>
     * @return The selected.
     */
    public boolean isSelected() {
        return selected; // returning selected.
    }

    /**
     * <p>This method can be used for setting the selected.</p>
     * @param selected The selected to set.
     */
    public void setSelected(boolean selected) {
        this.selected = selected; // Assigning to this.selected.
        invalidate();
    }

    /**
     * <p>This method can be used for setting the focusable.</p>
     * @param focusable The focusable to set.
     */
    public void setFocusable(boolean focusable) {
        this.focusable = focusable; // Assigning to this.focusable.
    }

    /**
     * <p>This method can be used for getting the width.</p>
     * @return The width.
     */
    public int getWidthAssigned() {
        return width; // returning width.
    }

    /**
     * <p>This method can be used for setting the width.</p>
     * @param width The width to set.
     */
    public void setWidthAssigned(int width) {
        this.width = width; // Assigning to this.width.
    }

    /**
     * <p>This method can be used for getting the hieght.</p>
     * @return The hieght.
     */
    public int getHeightAssigned() {
        return height; // returning hieght.
    }

    /**
     * <p>This method can be used for setting the hieght.</p>
     * @param hieght The hieght to set.
     */
    public void setHeightAssigned(int height) {
        this.height = height; // Assigning to this.hieght.
    }

    private int height = 0;
   
   
    /**
     * Constructor for ImageButton.
     */
    public ImageButton(Bitmap backgroundImage, Bitmap pressedImage, int width, int height, long style) {
        super(style);
        this.backgroundImage = backgroundImage;
        this.pressedImage = pressedImage;
        this.width = width;
        this.height = height;
    }

   
    public int getPreferredHeight() {
        if(getHeightAssigned() <= 0)
            return this.backgroundImage.getHeight();
        return getHeightAssigned();
    }

    public int getPreferredWidth() {
        if(getWidthAssigned() <= 0)
            return this.backgroundImage.getWidth();
        return getWidthAssigned();
    }

    /* (non-Javadoc)
     * @see net.rim.device.api.ui.Field#layout(int, int)
     */
    protected void layout(int width, int height) {
        setExtent(Math.min(width, getPreferredWidth()),
                Math.min(height, getPreferredHeight()));
    }

    /* (non-Javadoc)
     * @see net.rim.device.api.ui.Field#paint(net.rim.device.api.ui.Graphics)
     */
    protected void paint(Graphics graphics) {
        if ((isFocus() || isSelected()) && pressedImage != null) {
            graphics.drawBitmap(0, (getPreferredHeight() - pressedImage.getHeight())/2, getPreferredWidth(), getPreferredHeight(),
                    pressedImage, 0, 0);
        } else if (backgroundImage != null) {
            graphics.drawBitmap(0, (getPreferredHeight() - pressedImage.getHeight())/2, getPreferredWidth(), getPreferredHeight(),
                    backgroundImage, 0, 0);
        } else {
            graphics.setColor(Color.WHITE);
            graphics.fillRoundRect(1, 1, getWidth() - 2, getHeight() - 2, 12,
                    12);
        }
    }
   
    protected void drawFocus(Graphics graphics, boolean on) {
    }
   
    /* (non-Javadoc)
     * @see net.rim.device.api.ui.Field#isFocusable()
     */
    public boolean isFocusable() {
        return focusable;
    }
   
    protected void onFocus(int direction) {
        //  int fieldIndex = getLastFocusedCBIndex();
       //     CustomField field = (CustomField)getField(fieldIndex);
           
        super.onFocus(direction);
        invalidate();
    }

    protected void onUnfocus() {
        super.onUnfocus();
        invalidate();
    }
   
    protected boolean navigationClick(int status, int time) {
        fieldChangeNotify(0);
        return true;
    }
   
    protected boolean keyChar(char character, int status, int time) {
        if (character == Keypad.KEY_ENTER) {
            fieldChangeNotify(0);
            return true;
        }
        return super.keyChar(character, status, time);
    }

    /**
     * <p>This is the method for .</p>
     */
    public void focusLost() {
        onUnfocus();
        invalidate();
    }
}

Coverflow

  public CoverFlowDemoScreen()
    {     
        setTitle("Picture Scroll Field Demo");      
       
        // Initialize an array of Bitmaps
        _bitmaps = new Bitmap[NUM_ENTRIES];
        ScrollEntry[] entries = new ScrollEntry[NUM_ENTRIES];
        for(int i=0;i<30;i++){
             _bitmaps[i] = Bitmap.getBitmapResource(String.valueOf(i+1)+".jpg");
             entries[i] = new ScrollEntry(_bitmaps[i], "BlackBerry", "");
        }
       

        // Initialize the picture scroll field
        _pictureScrollField = new PictureScrollField(107, 156);
        _pictureScrollField.setData(entries, 0);       
        _pictureScrollField.setHighlightStyle(HighlightStyle.ILLUMINATE);
        _pictureScrollField.setHighlightBorderColor(Color.RED);
        _pictureScrollField.setBackground(BackgroundFactory.createSolidBackground(Color.BLACK));
        _pictureScrollField.setLabelsVisible(true);
        _pictureScrollField.setLensShrink(0.4f);
        add(_pictureScrollField);
       
        // Initialize a choice field for highlight style selection
    }   
   

Split String

    public static String[] splitString(String message, String delimiter)
    {
        Vector result = new Vector();
        int pos = 0;
        int len = message.length();
        int lenD = delimiter.length();
        for (int i = 0; i < len; i++)
        {
            if (message.substring(i, i+lenD).equals(delimiter))
            {
                if (i == pos)
                    result.addElement("");
                else if (i>pos)
                    result.addElement(message.substring(pos, i));
                pos = i+lenD;
                i += lenD-1;
            }
        }
        if (pos < len)
            result.addElement(message.substring(pos, len));
       
        String[] reposne = new String[result.size()];
        result.copyInto(reposne);
        return reposne;
    }

Trimmed Text With Respect to Screen Size

public static String getTrimmedTextRespectToScreen(String input, Font font, int widthAvailable){
        if(font.getAdvance(input) > widthAvailable){
                 int length = input.length();
            widthAvailable -= font.getAdvance("...");
            for (int i = 1; i < length; i++) {
                if(font.getAdvance(input, 0, i) >= widthAvailable){
                    return input.substring(0, i-1).trim() + "..";
                }
            }
        }
        return input;
    }

Copy File (SDCard)


    public static void copyFile(String sourceFile, String destinationFile)
    {
        try
        {
            FileConnection sourceFileConnector;
            FileConnection destinationFileConnector;

            sourceFileConnector = (FileConnection) Connector.open(sourceFile,Connector.READ_WRITE);
            destinationFileConnector = (FileConnection) Connector.open(destinationFile,Connector.READ_WRITE);
            if(!destinationFileConnector.exists()) // if file does not exists , create a new one
            {
                destinationFileConnector.create();
            }
            InputStream is = sourceFileConnector.openInputStream();
            OutputStream os =destinationFileConnector.openOutputStream();
            byte[] buf = new byte[1024];
            int len;
            while ((len = is.read(buf)) > 0)
            {
                os.write(buf, 0, len);
            }
            is.close();
            os.close();
        }
        catch(IOException e)
        {

        }
    }