Wednesday, 1 October 2014

last post continued.

i have extracted the code of login window from my project. see the code below and sorry for my bad explanation (what i will write in comments) in advance. :) please post comments if you have questions.

#include <gtk/gtk.h>
#include<stdio.h>
#include<stdlib.h>
#include <string.h>

struct login_window
   {
     GtkEntry *e1;
     GtkEntry *e2;
    GtkLabel *l1;
    GtkLabel *l2;
   }; /*i have grouped all the widgets in one structure. this technique will help if you have huge number of  windows in your project. as the number of window will increase the variable we declare will increase and the declared variables can have same name of different windows */

    typedef struct login_window widgets; /*so that i dont have to write struct login_window to                                                                                   declare variable. i will just write widgets*/


char pw[30];  //a globel variable to save password which we will get a from file.

//handlers of login_window buttons
G_MODULE_EXPORT void on_window1_destroy ()
  {
    gtk_main_quit ();//to safely close window when you hit the cross button of window.
  }

G_MODULE_EXPORT void on_button2_clicked ()
 {
   gtk_main_quit(); //window will close when quit button is clicked.
 }

void decrypt(int key)  /*the password i have stored in the file  is encrypted. i have used an                                                       encryption algorithm but i'm not pasting code here if you want the tell me.*/
{
    unsigned int i=0;
    FILE *fp;
    char temp;
    fp=fopen("password.txt","r"); // open file
    if(fp==NULL)                  // check if file is opened or not
     {
      printf("error");
      EXIT_FAILURE;
     }
    while((temp=fgetc(fp))!=EOF)  
     {
      pw[i++]=temp;  //copying content of file in an array
     }

    for(i=0;i<strlen(pw);++i)
    {
        pw[i] = pw[i] + key;  //decrypting with key i have used for encryption
    }
 fclose(fp);
}

/*code below is the handler for ok button. it is compulsory to prefix the handler with G_MODULE_EXPORT. otherwise you will get error that handler not found.*/

G_MODULE_EXPORT void on_button1_clicked ( GtkButton *button,widgets  *widg )
{
    char ch1[30],ch2[30],temp,un[30];
    int i=0,j=0,a;
    GtkWidget *dialog=NULL;

 const gchar *text1 = gtk_entry_get_text( widg->e2 ); /*e2 is a pointer of type get entry which i                                                                                                     have declared in the structure.widg is a                                                                                                     pointer of structure(widgets) type.
                                                                                             gtk_entry_get_text() is used to get text of                                                                                                  textbox */
  const gchar *text2 = gtk_entry_get_text( widg->e1 );

 strcpy(ch1,text1); //copying the text of textbox into an array
 strcpy(ch2,text2);

 FILE *fp1;
 fp1=fopen("user_name.txt","r");
 if(fp1==NULL)
  {
     printf("error");
     EXIT_FAILURE;
  }

  while((temp=fgetc(fp1))!=EOF)
  {
      un[i++]=temp;
  }

decrypt(0xFACA); 
  i=0;j=0;
  if((a=strcmp(un,ch1))==0)
  {
      printf("\nuser name is correct");
  }
  else
  {
      i++; //if name is not correct
  }
  if((a=strcmp(pw,ch2))==0)
  {
      printf("\npassword is correct");
  }
  else
  {
      j++; //if password is not correct
  }

  if(i>0||j>0)
  {                                 /*creating a dialog to display that username or password is wrong for more                                           info of these api's refer the website i have mentioned in the introduction part */                                         
  dialog = gtk_message_dialog_new (GTK_WINDOW (dialog), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "you have entered wrong user name or password");
  gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
  gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_destroy (dialog);
  }
  else
      {   // username and password is correct
   dialog = gtk_message_dialog_new (GTK_WINDOW (dialog), GTK_DIALOG_MODAL,  GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "login success");
  gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
  gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_destroy (dialog);

   }
}

int main(int argc, char *argv[])
{
    GtkBuilder *gtkBuilder; 
    widgets   widg;
    GtkWidget  *window;

    gtk_init(&argc, &argv);
    gtkBuilder = gtk_builder_new(); 

    gtk_builder_add_from_file(gtkBuilder, "login_window.glade", NULL);/*this api is used to                                                                build the glade file which we have created in glade UI designer.*/


    window = GTK_WIDGET( gtk_builder_get_object( gtkBuilder, "window1") );

//connecting each structure member to it appropriate gtkwidget
    widg.e1 = GTK_ENTRY( gtk_builder_get_object( gtkBuilder, "entry1" ) );
    widg.e2 = GTK_ENTRY( gtk_builder_get_object( gtkBuilder, "entry2" ) );
    widg.l1 = GTK_LABEL( gtk_builder_get_object( gtkBuilder, "label1" ) );
    widg.l2 = GTK_LABEL( gtk_builder_get_object( gtkBuilder, "label2" ) );

    gtk_builder_connect_signals( gtkBuilder, &widg ); /*connecting signals so that handlers can be                                                                                             called*/
    g_object_unref(G_OBJECT(gtkBuilder));
    gtk_widget_show(window);
    gtk_main(); //it is basically a loop to make window appear. if we not write it then window will                                   appear but disappear after few seconds automatically.*/
    return 0;
}

when running project, it will show window like in below image:




when username and password is correct below dialog will popup


when either username or password is wrong below dialog will popup




No comments:

Post a Comment