Set Target libraries in CMake

If you want to set target library for your library using CMake tool, use TARGET_LINK_LIBRARIES

For example, you have add the sqlite db operations in your c code. You need to link the sqlite3 library in your CMakeLists.txt.

Sample, given below,

TARGET_LINK_LIBRARIES(mylib sqlite3)

 

 

Updating progress bar control(GUI controls) from a thread function

Hi,

GUI controls should be updated only GUI thread. For updating controls from a thread function, you should invoke GUI thread for do this.
See the sample code below, for updating GUI controls from a thread function.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ThreadSample
{
    public partial class Form1 : Form
    {

        // This delegate enables asynchronous calls for setting
        // the text property on a Label control.
        delegate void SetTextCallback(string value);

 // This delegate enables asynchronous calls for setting
        // the value property on a progressbar control.
        delegate void SetProgressBarCallback(int value);

        public Form1()
        {
            InitializeComponent();

            EvaluateProgressBar();
        }

        private void EvaluateProgressBar()
        {
            Thread thread1 = new Thread(new ThreadStart(updateProgressBar));
            thread1.Start();
           
        }

        private void SetProgressBarValue(int value)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.progressBar1.InvokeRequired)
            {
                SetProgressBarCallback d = new SetProgressBarCallback(SetProgressBarValue);
                this.Invoke(d, new object[] { value });
            }
            else
            {
                this.progressBar1.Value= value;
            }
        }
        private void SetText(string value)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.label1.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { value });
            }
            else
            {
                this.label1.Text = value;
            }

        }
        private void updateProgressBar()
        {
            int i =0;
            while(true)
            {
                if (i == 100)
                    break;

                SetProgressBarValue(i);
                if (i == 0)
                {
                    SetText(“Initializing…”);
                   // Thread.Sleep(1000);
                }
                else if (i == 50)
                {
                    SetText(“Firmware updating…”);
                }
                else if (i == 75)
                {
                    SetText(“Firmware updated…”);
                    //Thread.Sleep(1000);
                }
                else if (i == 99)
                {
                    SetText(“completed…”);
                }
              
                i++;

                Thread.Sleep(1000);
            }
        }
    }
}

TeXnicCenter

TeXnicCenter is what we call an integrated documentation environment (IDE) for LaTeX. That means that TeXnicCenter integrates all the functionality you need to create, write, build, fix, view and print your LaTeX documents.

TeXnicCenter’s innovative user interface, its powerful features, its large community (more than 1,000,000 users all over the world) and the fact that it is Free Open Source Software (GPL) makes it the first choice for LaTeX authors working on the Windows platform.

For more read  : TexnicCenter

gvvTwitrl, A twitter desktop client in C#.Net using xAuth authentication

On last week I got an opportunity to implement a twitter client. I have implemented a twitter client in C#. I shall share with you these details.

Twitter is a social networking and microblogging service, owned and operated by Twitter Inc., that enables its users to send and read other users’ messages called tweets. Tweets are text-based posts of up to 140 characters displayed on the author’s profile page. Tweets are publicly visible by default, however senders can restrict message delivery to their friends list. Users may subscribe to other author tweets—this is known as following and subscribers are known as followers. As of late 2009, users can follow lists of authors instead of following individual authors.

We have to implement a twitter client in C#. Twitter has removed the basic authentication. So user should not be able to pass user name and password to twitter.  Auth mechanism is used to logging to twitter. If we are used auth, login dialog, authorization dialog should be displayed by twitter. We have no role in that part. When we shall used xAuth mechanism to log into twitter, this mechanism should support direct exchanging user name and password to twitter.  xAuth provides a way for desktop and mobile applications to exchange a username and password for an OAuth access token. Once the access token is retrieved, xAuth-enabled developers should dispose of the login and password corresponding to the user. For this, we should do some steps

  1. First sign into twitter account, and register a new application.Enter details of the application, make sure that Application type and Default access type.

ü  Application type should be Client.

ü  Default Access Type should be Read & Write.

After enter the valid entries, register the application.

2  When successfully registered application, you will get a consumer key and consumer  secret . xAuth is not enabled for applications by default. xAuth access is restricted to approved applications. To use xAuth, send a request to api@twitter.com with plenty of details about your application and why xAuth is the best choice for it. Include the name of your application, the consumer key, the application ID (if available), and a summary of how xAuth is best-suited for your application. xAuth allows desktop and mobile applications to skip the request_token and authorize steps and jump right to the access_token step.

3. Next step is access token step, for this used the below url http://api.twitter.com/oauth/access_token.

Code is added below,keep the consumer key and consumer secret in a config file.

public enum Method { GET, POST };

public const string REQUEST_TOKEN = “http://twitter.com/oauth/request_token”;

public const string AUTHORIZE = “http://twitter.com/oauth/authorize”;

public const string ACCESS_TOKEN = “http://twitter.com/oauth/access_token”;

public const string XAUTH_ACCESS_TOKEN = “https://api.twitter.com/oauth/access_token”;

private string _consumerKey    = “”;

private string _consumerSecret = “”;

private string _token          = “”;

private string _tokenSecret    = “”;

private string _verifier       = “”;

private string _xAuthUsername  = “”;

private string _xAuthPassword = “”;

/// <summary>
/// Exchange the username and password for an access token.
/// </summary>
/// <param name=”username”>Twitter Username.</param>
/// <param name=”username”>Twitter Password.</param>
public void xAuthAccessTokenGet(string username, string password)
{
try
{
if (!string.IsNullOrEmpty(username.Trim()) && !string.IsNullOrEmpty(password))
{
this.xAuthUsername = username;
this.xAuthPassword = password;

// string response = oAuthWebRequest(Method.GET, XAUTH_ACCESS_TOKEN, String.Empty);
string response = oAuthWebRequest(Method.POST, XAUTH_ACCESS_TOKEN, String.Empty);

if (response.Length > 0)
{
//Store the Token and Token Secret
NameValueCollection qs = HttpUtility.ParseQueryString(response);
if (qs["oauth_token"] != null)
{
this.Token = qs["oauth_token"];
}
if (qs["oauth_token_secret"] != null)
{
this.TokenSecret = qs["oauth_token_secret"];
}
}
}
else
{
MessageBox.Show(“Please enter a valid username or password”, “Twitter”);
}
}
catch (Exception ex)
{
if (ex.Message.Contains(“UnAuthorized”))
{
MessageBox.Show(“Invalid username and/or password”, “Twitter Login”);
}
else if (ex.Message.Contains(“Service Unavailable”))
{
MessageBox.Show(“Too many people tweeting.. Try again later.”, “Twitter Login”);
}
else
{
MessageBox.Show(ex.Message, “Twitter login”);
}
}
}

/// <summary>
/// Submit a web request using oAuth.
/// </summary>
/// <param name=”method”>GET or POST</param>
/// <param name=”url”>The full url, including the querystring.</param>
/// <param name=”postData”>Data to post (querystring format)</param>
/// <returns>The web server response.</returns>
public string oAuthWebRequest(Method method, string url, string postData)
{
string outUrl = “”;
string querystring = “”;
string ret = “”;
//Setup postData for signing.
//Add the postData to the querystring.
if (method == Method.POST)
{
if (postData.Length > 0)
{
//Decode the parameters and re-encode using the oAuth UrlEncode method.
NameValueCollection qs = HttpUtility.ParseQueryString(postData);
postData = “”;
foreach (string key in qs.AllKeys)
{
if (postData.Length > 0)
{
postData += “&”;
}
qs[key] = HttpUtility.UrlDecode(qs[key]);
qs[key] = this.UrlEncode(qs[key]);
postData += key + “=” + qs[key];

}
if (url.IndexOf(“?”) > 0)
{
url += “&”;
}
else
{
url += “?”;
}
url += postData;
url.Trim();
}
}

Uri uri = new Uri(url);

string nonce = this.GenerateNonce();
string timeStamp = this.GenerateTimeStamp();

//Generate Signature
string sig = this.GenerateSignature(uri,
this.ConsumerKey,
this.ConsumerSecret,
this.Token,
this.TokenSecret,
this.Verifier,
this.xAuthUsername,
this.xAuthPassword,
method.ToString(),
timeStamp,
nonce,
out outUrl,
out querystring);

querystring += “&oauth_signature=” + HttpUtility.UrlEncode(sig);

//Convert the querystring to postData
if (method == Method.POST)
{
postData = querystring;
querystring = “”;
}

if (querystring.Length > 0)
{
outUrl += “?”;
}

ret = WebRequest(method, outUrl +  querystring, postData);

return ret;
}

/// <summary>
/// Web Request Wrapper
/// </summary>
/// <param name=”method”>Http Method</param>
/// <param name=”url”>Full url to the web resource</param>
/// <param name=”postData”>Data to post in querystring format</param>
/// <returns>The web server response.</returns>
public string WebRequest(Method method, string url, string postData)
{
HttpWebRequest webRequest = null;
StreamWriter requestWriter = null;
string responseData = “”;

webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
webRequest.Method = method.ToString();
webRequest.ServicePoint.Expect100Continue = false;
//webRequest.UserAgent  = “Identify your application please.”;
//webRequest.Timeout = 20000;

if (method == Method.POST)
{
webRequest.ContentType = “application/x-www-form-urlencoded”;

//POST the data.
requestWriter = new StreamWriter(webRequest.GetRequestStream());
try
{
requestWriter.Write(postData);
}
catch
{
throw;
}
finally
{
requestWriter.Close();
requestWriter = null;
}
}

responseData = WebResponseGet(webRequest);

webRequest = null;

return responseData;

}

/// <summary>
/// Process the web response.
/// </summary>
/// <param name=”webRequest”>The request object.</param>
/// <returns>The response data.</returns>
public string WebResponseGet(HttpWebRequest webRequest)
{
StreamReader responseReader = null;
string responseData = “”;

try
{
WebResponse re=   webRequest.GetResponse();
responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
responseData = responseReader.ReadToEnd();
}
catch(Exception ex)
{
//throw;
MessageBox.Show(ex.Message);
}
finally
{
webRequest.GetResponse().GetResponseStream().Close();
responseReader.Close();
responseReader = null;
}

return responseData;

}

4. The sample application looks like below. Using this application user should be able to log into twitter . I have also implemented other twitter functionalities like To get followers details, to get friends details, To post a tweet, To search a specified user, To log out the session. To refer more details ,http://dev.twitter.com/doc

I hope this article should be help ful every body. Please let me know if any concern about this.