Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.56 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #include <gtk/gtk.h>
  4.  
  5. #include <time.h>
  6.  
  7. #include <curses.h>
  8.  
  9. #include <sys/types.h>
  10.  
  11. #include <sys/socket.h>
  12.  
  13. #include <netinet/in.h>
  14.  
  15. #include <arpa/inet.h>
  16.  
  17. #include <netdb.h>
  18.  
  19. #include <string.h>
  20.  
  21. #include <unistd.h>
  22.  
  23. #include <stdlib.h>
  24.  
  25. #include <pthread.h>
  26.  
  27. #include <assert.h>
  28.  
  29.  
  30.  
  31. GtkListStore * list_rooms;
  32.  
  33. GtkListStore * list_users;
  34.  
  35. GtkListStore * list_messages;
  36.  
  37.  
  38.  
  39. char * host = "localhost";
  40.  
  41. char * user = (char *) malloc (20 * sizeof(char));
  42.  
  43. char * password = (char *) malloc (20 * sizeof(char));
  44.  
  45. char * message = (char *) malloc (300 * sizeof(char));
  46.  
  47. char * tmessage = (char *) malloc (200 * sizeof(char));
  48.  
  49. char * room = (char *) malloc (50 * sizeof(char));
  50.  
  51. char * sport = (char *) malloc (20 * sizeof(char));
  52.  
  53. char * args = (char *) malloc (300 * sizeof(char));
  54.  
  55. int port = 2038;
  56.  
  57. int roomCount = 0;
  58.  
  59. int log_user = 0;
  60.  
  61. int log_room = 0;
  62.  
  63. int log_messages = 0;
  64.  
  65. int timesdone = 0;
  66.  
  67. char *preval;
  68.  
  69.  
  70.  
  71. GtkWidget *tree_view;
  72.  
  73. GtkTreeSelection *selection;
  74.  
  75.  
  76.  
  77. #define MAX_MESSAGES 100
  78.  
  79. #define MAX_MESSAGE_LEN 300
  80.  
  81. #define MAX_RESPONSE (20 * 1024)
  82.  
  83.  
  84.  
  85. int lastMessage = 0;
  86.  
  87. int timesRun = 0;
  88.  
  89.  
  90.  
  91.  
  92.  
  93. int open_client_socket(char * host, int port) {
  94.  
  95. // Initialize socket address structure
  96.  
  97. struct sockaddr_in socketAddress;
  98.  
  99.  
  100.  
  101. // Clear sockaddr structure
  102.  
  103. memset((char *)&socketAddress, 0, sizeof(socketAddress));
  104.  
  105.  
  106.  
  107. // Set family to Internet
  108.  
  109. socketAddress.sin_family = AF_INET;
  110.  
  111.  
  112.  
  113. // Set port
  114.  
  115. socketAddress.sin_port = htons((u_short)port);
  116.  
  117.  
  118.  
  119. // Get host table entry for this host
  120.  
  121. struct hostent *ptrh = gethostbyname(host);
  122.  
  123. if ( ptrh == NULL ) {
  124.  
  125. perror("gethostbyname");
  126.  
  127. exit(1);
  128.  
  129. }
  130.  
  131.  
  132.  
  133. // Copy the host ip address to socket address structure
  134.  
  135. memcpy(&socketAddress.sin_addr, ptrh->h_addr, ptrh->h_length);
  136.  
  137.  
  138.  
  139. // Get TCP transport protocol entry
  140.  
  141. struct protoent *ptrp = getprotobyname("tcp");
  142.  
  143. if ( ptrp == NULL ) {
  144.  
  145. perror("getprotobyname");
  146.  
  147. exit(1);
  148.  
  149. }
  150.  
  151.  
  152.  
  153. // Create a tcp socket
  154.  
  155. int sock = socket(PF_INET, SOCK_STREAM, ptrp->p_proto);
  156.  
  157. if (sock < 0) {
  158.  
  159. perror("socket");
  160.  
  161. exit(1);
  162.  
  163. }
  164.  
  165.  
  166.  
  167. // Connect the socket to the specified server
  168.  
  169. if (connect(sock, (struct sockaddr *)&socketAddress,
  170.  
  171. sizeof(socketAddress)) < 0) {
  172.  
  173. perror("connect");
  174.  
  175. exit(1);
  176.  
  177. }
  178.  
  179.  
  180.  
  181. return sock;
  182.  
  183. }
  184.  
  185.  
  186.  
  187. int sendCommand(char * host, int port, char * command, char * user,
  188.  
  189. char * password, char * args, char * response) {
  190.  
  191. int sock = open_client_socket( host, port);
  192.  
  193.  
  194.  
  195. // Send command
  196.  
  197. write(sock, command, strlen(command));
  198.  
  199. write(sock, " ", 1);
  200.  
  201. write(sock, user, strlen(user));
  202.  
  203. write(sock, " ", 1);
  204.  
  205. write(sock, password, strlen(password));
  206.  
  207. write(sock, " ", 1);
  208.  
  209. write(sock, args, strlen(args));
  210.  
  211. write(sock, "\r\n",2);
  212.  
  213.  
  214.  
  215. // Keep reading until connection is closed or MAX_REPONSE
  216.  
  217. int n = 0;
  218.  
  219. int len = 0;
  220.  
  221. while ((n=read(sock, response+len, MAX_RESPONSE - len))>0) {
  222.  
  223. len += n;
  224.  
  225. }
  226.  
  227.  
  228.  
  229. //printf("response:%s\n", response);
  230.  
  231.  
  232.  
  233. close(sock);
  234.  
  235. }
  236.  
  237.  
  238.  
  239.  
  240.  
  241. /*
  242.  
  243. void get_messages() {
  244.  
  245. char response[ MAX_RESPONSE ];
  246.  
  247. sendCommand(host, port, "GET-MESSAGES", user, password, room, "", response);
  248.  
  249. if (!strcmp(response,"OK\r\n")) {
  250.  
  251. printf("Room %s created\n", room);
  252.  
  253. roomCount++;
  254.  
  255. }
  256.  
  257. }*/
  258.  
  259.  
  260.  
  261. void send_message() {
  262.  
  263. char response[ MAX_RESPONSE ] = {0};
  264.  
  265. response[0] = '\0';
  266.  
  267. args = strdup(room);
  268.  
  269. strcat(args, " ");
  270.  
  271. strcat(args, message);
  272.  
  273. sendCommand(host, port, "SEND-MESSAGE", user, password, args, response);
  274.  
  275. if (!strcmp(response,"OK\r\n")) {
  276.  
  277. //log_messages = 1;
  278.  
  279. printf("Message %s sent\n", args);
  280.  
  281. }
  282.  
  283. }
  284.  
  285. /*
  286.  
  287. void send_message_mod(char * argument) {
  288.  
  289. char response[ MAX_RESPONSE ];
  290.  
  291. response[0] = '\0';
  292.  
  293. args = strdup(room);
  294.  
  295. strcat(args, argument);
  296.  
  297. sendCommand(host, port, "SEND-MESSAGE", user, password, args, response);
  298.  
  299. if (!strcmp(response,"OK\r\n")) {
  300.  
  301. //log_messages = 1;
  302.  
  303. printf("Message %s sent\n", args);
  304.  
  305. }
  306.  
  307. }*/
  308.  
  309.  
  310.  
  311. void leave_room(char * troom) {
  312.  
  313. char response[ MAX_RESPONSE ] = {0};
  314.  
  315. response[0] = '\0';
  316.  
  317. strcpy(args, troom);
  318.  
  319. //char * tt = (char *) malloc (100 * sizeof(char));
  320.  
  321. sendCommand(host, port, "LEAVE-ROOM", user, password, args, response);
  322.  
  323. if (!strcmp(response,"OK\r\n")) {
  324.  
  325. //tt = strdup(user);
  326.  
  327. //strcat(tt, " has left the room");
  328.  
  329. //send_message_mod(tt);
  330.  
  331. printf("User %s removed\n", user);
  332.  
  333. }
  334.  
  335. }
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343. void update_list_rooms() {
  344.  
  345. GtkTreeIter iter;
  346.  
  347. int i;
  348.  
  349. char * temp = (char *) malloc (30 * sizeof(char));
  350.  
  351. gchar *msg;
  352.  
  353. //timesdone = 0;
  354.  
  355. /* Add some messages to the window */
  356.  
  357. if (log_user == 1) {
  358.  
  359. char responserooms[ MAX_RESPONSE ] = {0};
  360.  
  361. responserooms[0] = '\0';
  362.  
  363. sendCommand(host, port, "LIST-ROOMS", user, password, "default", responserooms);
  364.  
  365. printf("response: %s\n", responserooms);
  366.  
  367. temp = strtok(responserooms, "\r\n");
  368.  
  369. msg = g_strdup_printf ("%s", temp);
  370.  
  371. gtk_list_store_append (GTK_LIST_STORE (list_rooms), &iter);
  372.  
  373. gtk_list_store_set (GTK_LIST_STORE (list_rooms),
  374.  
  375. &iter,
  376.  
  377. 0, msg,
  378.  
  379. -1);
  380.  
  381. roomCount++;
  382.  
  383.  
  384.  
  385. while ((temp = strtok(NULL, "\r\n")) != NULL) {
  386.  
  387.  
  388.  
  389. msg = g_strdup_printf ("%s", temp);
  390.  
  391. printf("ROOMS %s\n", msg);
  392.  
  393. gtk_list_store_append (GTK_LIST_STORE (list_rooms), &iter);
  394.  
  395. gtk_list_store_set (GTK_LIST_STORE (list_rooms),
  396.  
  397. &iter,
  398.  
  399. 0, msg,
  400.  
  401. -1);
  402.  
  403. roomCount++;
  404.  
  405. }
  406.  
  407. //g_free(msg);
  408.  
  409. }
  410.  
  411. /*if (log_user == 1) {
  412.  
  413. gchar *msg = g_strdup_printf ("%s", room);
  414.  
  415. gtk_list_store_append (GTK_LIST_STORE (list_rooms), &iter);
  416.  
  417. gtk_list_store_set (GTK_LIST_STORE (list_rooms),
  418.  
  419. &iter,
  420.  
  421. 0, msg,
  422.  
  423. -1);
  424.  
  425. }*/
  426.  
  427. }
  428.  
  429.  
  430.  
  431. void create_room() {
  432.  
  433. char response[ MAX_RESPONSE ] = {0};
  434.  
  435. response[0] = '\0';
  436.  
  437. sendCommand(host, port, "CREATE-ROOM", user, password, room, response);
  438.  
  439. if (!strcmp(response,"NO\r\n")) {
  440.  
  441. printf("Room couldn't be added cause you fool\n");
  442.  
  443. log_room = 1;
  444.  
  445. return;
  446.  
  447. } else if (!strcmp(response, "OK\r\n")) {
  448.  
  449. log_room = 1;
  450.  
  451. printf("Room %s added\n", room);
  452.  
  453. //log_user = 1;
  454.  
  455. //gtk_list_store_clear(GTK_LIST_STORE(list_rooms));
  456.  
  457. //update_list_rooms();
  458.  
  459. }
  460.  
  461. //sendCommand(host, port, "LIST-ROOMS", user, password, "", "", response);
  462.  
  463. //printf("%s\n", response);
  464.  
  465. }
  466.  
  467.  
  468.  
  469. void enter_room() {
  470.  
  471. char response[ MAX_RESPONSE ] = {0};
  472.  
  473. response[0] = '\0';
  474.  
  475. //char * tt = (char *) malloc (100 * sizeof(char));
  476.  
  477. sendCommand(host, port, "ENTER-ROOM", user, password, room, response);
  478.  
  479. if (!strcmp(response, "OK\r\n")) {
  480.  
  481. //tt = strdup(user);
  482.  
  483. //strcat(tt, " has entered the room");
  484.  
  485. //send_message_mod(tt);
  486.  
  487. //sendCommand(host, port, "ENTER-ROOM", user, password, "User ", response);
  488.  
  489. gtk_list_store_clear(GTK_LIST_STORE(list_rooms));
  490.  
  491. strcat(message, " has joined room");
  492.  
  493. send_message();
  494.  
  495. gtk_list_store_clear(GTK_LIST_STORE(list_rooms));
  496.  
  497. //send_message_room();
  498.  
  499. update_list_rooms();
  500.  
  501. printf("User %s added\n", user);
  502.  
  503. }
  504.  
  505. }
  506.  
  507.  
  508.  
  509. /*void get_users_in_room(char * troom) {
  510.  
  511. char response[ MAX_RESPONSE ];
  512.  
  513. sendCommand(host, port, "ENTER-ROOM", user, password, troom, "", response);
  514.  
  515. }*/
  516.  
  517.  
  518.  
  519. void update_list_users() {
  520.  
  521. GtkTreeIter iterator;
  522.  
  523. int i;
  524.  
  525. char * temp = (char *) malloc (30 * sizeof(char));
  526.  
  527. gchar *msg;
  528.  
  529. //Add some messages to the window
  530.  
  531. if (log_user == 1) {
  532.  
  533. char responseuser[ MAX_RESPONSE ] = {0};
  534.  
  535. responseuser[0] = '\0';
  536.  
  537. if (room != NULL) {
  538.  
  539. sendCommand(host, port, "GET-USERS-IN-ROOM", user, password, room, responseuser);
  540.  
  541. temp = strdup(responseuser);
  542.  
  543. temp = strtok(temp, "\r\n");
  544.  
  545. msg = g_strdup_printf ("%s", temp);
  546.  
  547. gtk_list_store_append (GTK_LIST_STORE (list_users), &iterator);
  548.  
  549. gtk_list_store_set (GTK_LIST_STORE (list_users),
  550.  
  551. &iterator,
  552.  
  553. 0, msg,
  554.  
  555. -1);
  556.  
  557. while ((temp = strtok(NULL, "\r\n")) != NULL) {
  558.  
  559. msg = g_strdup_printf ("%s", temp);
  560.  
  561. gtk_list_store_append (GTK_LIST_STORE (list_users), &iterator);
  562.  
  563. gtk_list_store_set (GTK_LIST_STORE (list_users),
  564.  
  565. &iterator,
  566.  
  567. 0, msg,
  568.  
  569. -1);
  570.  
  571. }
  572.  
  573. }
  574.  
  575. //g_free(msg);
  576.  
  577. }
  578.  
  579. }
  580.  
  581.  
  582.  
  583. void add_user() {
  584.  
  585. // Try first to add user in case it does not exist.
  586.  
  587. char responseadduser[ MAX_RESPONSE ] = {0};
  588.  
  589. responseadduser[0] = '\0';
  590.  
  591. sendCommand(host, port, "ADD-USER", user, password, "default", responseadduser);
  592.  
  593. if (!strcmp(responseadduser, "OK\r\n")) {
  594.  
  595. printf("User %s added\n", user);
  596.  
  597. log_user = 1;
  598.  
  599. //update_list_users();
  600.  
  601. //update_list_rooms();
  602.  
  603. } else if (!strcmp(responseadduser, "NO\r\n")) {
  604.  
  605. printf("User %s logged in\n", user);
  606.  
  607. log_user = 1;
  608.  
  609. //update_list_users();
  610.  
  611. //update_list_rooms();
  612.  
  613. }
  614.  
  615. }
  616.  
  617.  
  618.  
  619. /* Create the list of "messages" */
  620.  
  621. static GtkWidget *create_list( const char * titleColumn, GtkListStore *model )
  622.  
  623. {
  624.  
  625. GtkWidget *scrolled_window;
  626.  
  627. //GtkListStore *model;
  628.  
  629. GtkCellRenderer *cell;
  630.  
  631. GtkTreeViewColumn *column;
  632.  
  633.  
  634.  
  635. int i;
  636.  
  637.  
  638.  
  639. /* Create a new scrolled window, with scrollbars only if needed */
  640.  
  641. scrolled_window = gtk_scrolled_window_new (NULL, NULL);
  642.  
  643. gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
  644.  
  645. GTK_POLICY_AUTOMATIC,
  646.  
  647. GTK_POLICY_AUTOMATIC);
  648.  
  649.  
  650.  
  651. //model = gtk_list_store_new (1, G_TYPE_STRING);
  652.  
  653. tree_view = gtk_tree_view_new ();
  654.  
  655. gtk_container_add (GTK_CONTAINER (scrolled_window), tree_view);
  656.  
  657. gtk_tree_view_set_model (GTK_TREE_VIEW (tree_view), GTK_TREE_MODEL (model));
  658.  
  659. gtk_widget_show (tree_view);
  660.  
  661.  
  662.  
  663. cell = gtk_cell_renderer_text_new ();
  664.  
  665.  
  666.  
  667. column = gtk_tree_view_column_new_with_attributes (titleColumn,
  668.  
  669. cell,
  670.  
  671. "text", 0,
  672.  
  673. NULL);
  674.  
  675.  
  676.  
  677. gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view),
  678.  
  679. GTK_TREE_VIEW_COLUMN (column));
  680.  
  681.  
  682.  
  683. return scrolled_window;
  684.  
  685. }
  686.  
  687.  
  688.  
  689. /* Add some text to our text widget - this is a callback that is invoked
  690.  
  691. when our window is realized. We could also force our window to be
  692.  
  693. realized with gtk_widget_realize, but it would have to be part of
  694.  
  695. a hierarchy first */
  696.  
  697.  
  698.  
  699. static void insert_text( GtkTextBuffer *buffer, const char * initialText )
  700.  
  701. {
  702.  
  703. GtkTextIter iter;
  704.  
  705. gtk_text_buffer_get_iter_at_offset (buffer, &iter, 0);
  706.  
  707. gtk_text_buffer_insert (buffer, &iter, initialText,-1);
  708.  
  709. }
  710.  
  711.  
  712.  
  713. /* Create a scrolled text area that displays a "message" */
  714.  
  715. static GtkWidget *create_text( const char * initialText )
  716.  
  717. {
  718.  
  719. GtkWidget *scrolled_window;
  720.  
  721. GtkWidget *view;
  722.  
  723. GtkTextBuffer *buffer;
  724.  
  725.  
  726.  
  727. view = gtk_text_view_new ();
  728.  
  729. buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
  730.  
  731.  
  732.  
  733. scrolled_window = gtk_scrolled_window_new (NULL, NULL);
  734.  
  735. gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
  736.  
  737. GTK_POLICY_AUTOMATIC,
  738.  
  739. GTK_POLICY_AUTOMATIC);
  740.  
  741.  
  742.  
  743. gtk_container_add (GTK_CONTAINER (scrolled_window), view);
  744.  
  745. insert_text (buffer, initialText);
  746.  
  747.  
  748.  
  749. gtk_widget_show_all (scrolled_window);
  750.  
  751.  
  752.  
  753. return scrolled_window;
  754.  
  755. }
  756.  
  757.  
  758.  
  759. static void enteruser_callback(GtkWidget *entry) {
  760.  
  761. const gchar *entry_text;
  762.  
  763. entry_text = gtk_entry_get_text (GTK_ENTRY (entry));
  764.  
  765. user = strdup(entry_text);
  766.  
  767. printf("Entry contents: %s\n", entry_text);
  768.  
  769. }
  770.  
  771.  
  772.  
  773. static void enterpass_callback(GtkWidget *entry) {
  774.  
  775. const gchar *entry_text;
  776.  
  777. entry_text = gtk_entry_get_text (GTK_ENTRY (entry));
  778.  
  779. password = strdup(entry_text);
  780.  
  781. add_user();
  782.  
  783. printf("Entry contents: %s\n", entry_text);
  784.  
  785. }
  786.  
  787.  
  788.  
  789. static void enterroom_callback(GtkWidget *entry) {
  790.  
  791. const gchar *entry_text;
  792.  
  793. entry_text = gtk_entry_get_text (GTK_ENTRY (entry));
  794.  
  795. room = strdup(entry_text);
  796.  
  797. create_room();
  798.  
  799. printf("Entry contents: %s\n", entry_text);
  800.  
  801. }
  802.  
  803.  
  804.  
  805. static void on_changed(GtkWidget * widget, GtkWidget * Widgetone) {
  806.  
  807. //get the new room name and leave user from previous room
  808.  
  809. GtkTreeIter iter;
  810.  
  811. GtkTreeModel *model;
  812.  
  813. char * value;
  814.  
  815. //printf("Sefdf\n");
  816.  
  817. if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(selection), &model, &iter)) {
  818.  
  819. gtk_tree_model_get(model, &iter, 0, &value, -1);
  820.  
  821. printf("%s\n", value);
  822.  
  823. room = strdup(value);
  824.  
  825. if (preval == NULL) {
  826.  
  827. printf("preval is same\n");
  828.  
  829. preval = strdup(value);
  830.  
  831. log_messages = 1;
  832.  
  833. enter_room();
  834.  
  835. //gtk_list_store_clear(GTK_LIST_STORE(list_users));
  836.  
  837. //update_list_users();
  838.  
  839. } else {
  840.  
  841. printf("%s\n", preval);
  842.  
  843. leave_room(preval);
  844.  
  845. //strcat(message, " has left room");
  846.  
  847. //_message();
  848.  
  849. enter_room();
  850.  
  851. log_messages = 1;
  852.  
  853. gtk_list_store_clear(GTK_LIST_STORE(list_users));
  854.  
  855. update_list_users();
  856.  
  857. }
  858.  
  859. preval = strdup(value);
  860.  
  861. //gtk_label_set_text(GTK_LABEL(label), value);
  862.  
  863. //g_free(value);
  864.  
  865. }
  866.  
  867. //get new room through selection
  868.  
  869.  
  870.  
  871. //get previous selection - call leaveuser on it and change the prev selection
  872.  
  873. }
  874.  
  875.  
  876.  
  877. static void update_list_messages() {
  878.  
  879. GtkTreeIter iterators;
  880.  
  881. int i;
  882.  
  883. char * temp = (char *) malloc (300 * sizeof(char));
  884.  
  885. gchar *msg;
  886.  
  887. //Add some messages to the window
  888.  
  889. if (log_messages == 1) {
  890.  
  891. char responsemessage[ MAX_RESPONSE ] = {0};
  892.  
  893. responsemessage[0] = '\0';
  894.  
  895. sendCommand(host, port, "GET-MESSAGES", user, password, room, responsemessage);
  896.  
  897. temp = strdup(responsemessage);
  898.  
  899. temp = strtok(temp, "\r\n");
  900.  
  901. msg = g_strdup_printf ("%s", temp);
  902.  
  903. gtk_list_store_append (GTK_LIST_STORE (list_messages), &iterators);
  904.  
  905. gtk_list_store_set (GTK_LIST_STORE (list_messages),
  906.  
  907. &iterators,
  908.  
  909. 0, msg,
  910.  
  911. -1);
  912.  
  913. while ((temp = strtok(NULL, "\r\n")) != NULL) {
  914.  
  915. msg = g_strdup_printf ("%s", temp);
  916.  
  917. gtk_list_store_append (GTK_LIST_STORE (list_messages), &iterators);
  918.  
  919. gtk_list_store_set (GTK_LIST_STORE (list_messages),
  920.  
  921. &iterators,
  922.  
  923. 0, msg,
  924.  
  925. -1);
  926.  
  927. }
  928.  
  929. //g_free(msg);
  930.  
  931. }
  932.  
  933. }
  934.  
  935.  
  936.  
  937. static void get_messages() {
  938.  
  939. gtk_list_store_clear(GTK_LIST_STORE(list_messages));
  940.  
  941. update_list_messages();
  942.  
  943. }
  944.  
  945.  
  946.  
  947. static void message_callback(GtkWidget *entry) {
  948.  
  949. const gchar *entry_text;
  950.  
  951. entry_text = gtk_entry_get_text (GTK_ENTRY (entry));
  952.  
  953. message = strdup(entry_text);
  954.  
  955. log_messages = 1;
  956.  
  957. send_message();
  958.  
  959. printf("%s\n", room);
  960.  
  961. //get_messages();
  962.  
  963.  
  964.  
  965. printf("Entry contents: %s\n", entry_text);
  966.  
  967. }
  968.  
  969.  
  970.  
  971. static void initialize() {
  972.  
  973. timesdone = 0;
  974.  
  975. preval = NULL;
  976.  
  977. }
  978.  
  979.  
  980.  
  981. static gboolean
  982.  
  983. time_handler(GtkWidget *widget)
  984.  
  985. {
  986.  
  987. gtk_list_store_clear(GTK_LIST_STORE(list_rooms));
  988.  
  989. update_list_rooms();
  990.  
  991. gtk_list_store_clear(GTK_LIST_STORE(list_users));
  992.  
  993. update_list_users();
  994.  
  995. get_messages();
  996.  
  997. return TRUE;
  998.  
  999. }
  1000.  
  1001.  
  1002.  
  1003. int main( int argc,
  1004.  
  1005. char *argv[] )
  1006.  
  1007. {
  1008.  
  1009. GtkWidget *window;
  1010.  
  1011. GtkWidget *list;
  1012.  
  1013. GtkWidget *messages;
  1014.  
  1015. GtkWidget *myMessage;
  1016.  
  1017. GtkWidget *msguser;
  1018.  
  1019. GtkWidget *msgpass;
  1020.  
  1021. GtkWidget *msgroom;
  1022.  
  1023. GtkWidget *hbox, *vbox;
  1024.  
  1025. GtkWidget *entryuser;
  1026.  
  1027. GtkWidget *entrypass;
  1028.  
  1029. GtkWidget *entryroom;
  1030.  
  1031. GtkWidget *check;
  1032.  
  1033. GtkWidget *sendmessage;
  1034.  
  1035. GtkWidget * frameuser;
  1036.  
  1037. GtkWidget * frameroom;
  1038.  
  1039. gint tmp_pos;
  1040.  
  1041.  
  1042.  
  1043. gtk_init (&argc, &argv);
  1044.  
  1045.  
  1046.  
  1047. GdkColor color;
  1048.  
  1049. gdk_color_parse ("#159AC1", &color);
  1050.  
  1051. window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  1052.  
  1053. gtk_window_set_title (GTK_WINDOW (window), "IRC Client");
  1054.  
  1055. gtk_widget_modify_bg(window, GTK_STATE_NORMAL, &color);
  1056.  
  1057. g_signal_connect (window, "destroy",
  1058.  
  1059. G_CALLBACK (gtk_main_quit), NULL);
  1060.  
  1061. gtk_container_set_border_width (GTK_CONTAINER (window), 10);
  1062.  
  1063. gtk_widget_set_size_request (GTK_WIDGET (window), 950, 800);
  1064.  
  1065.  
  1066.  
  1067. //initialize
  1068.  
  1069. initialize();
  1070.  
  1071.  
  1072.  
  1073. // Create a table to place the widgets. Use a 7x4 Grid (7 rows x 4 columns)
  1074.  
  1075. GtkWidget *table = gtk_table_new (20, 6, TRUE);
  1076.  
  1077. gtk_container_add (GTK_CONTAINER (window), table);
  1078.  
  1079. gtk_table_set_row_spacings(GTK_TABLE (table), 6);
  1080.  
  1081. gtk_table_set_col_spacings(GTK_TABLE (table), 6);
  1082.  
  1083. gtk_widget_show (table);
  1084.  
  1085.  
  1086.  
  1087. // Add list of rooms. Use columns 0 to 4 (exclusive) and rows 0 to 4 (exclusive)
  1088.  
  1089. list_rooms = gtk_list_store_new (1, G_TYPE_STRING);
  1090.  
  1091. //update_list_rooms();
  1092.  
  1093. list = create_list ("Rooms", list_rooms);
  1094.  
  1095. gtk_table_attach_defaults (GTK_TABLE (table), list, 2, 4, 0, 9);
  1096.  
  1097. selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view));
  1098.  
  1099. g_signal_connect(tree_view, "row-activated",
  1100.  
  1101. G_CALLBACK(on_changed), selection);
  1102.  
  1103. gtk_widget_show (list);
  1104.  
  1105.  
  1106.  
  1107. // Add list of users. Use columns 0 to 4 and rows 0 to 4
  1108.  
  1109. list_users = gtk_list_store_new (1, G_TYPE_STRING);
  1110.  
  1111. //update_list_users();
  1112.  
  1113. list = create_list ("Users", list_users);
  1114.  
  1115. gtk_table_attach_defaults (GTK_TABLE (table), list, 4, 6, 0, 9);
  1116.  
  1117. gtk_widget_show (list);
  1118.  
  1119.  
  1120.  
  1121. // Add send button. Use columns 0 to 1 (exclusive) and rows 4 to 7 (exclusive)
  1122.  
  1123. GtkWidget *send_button = gtk_button_new_with_label ("Send");
  1124.  
  1125. gtk_table_attach_defaults(GTK_TABLE (table), send_button, 2, 4, 18, 20);
  1126.  
  1127. //gtk_widget_set_size_request(send_button, 80, 33);
  1128.  
  1129. //g_signal_connect_swapped (send_button, "clicked",
  1130.  
  1131. // G_CALLBACK (get_messages),
  1132.  
  1133. // window);
  1134.  
  1135. gtk_widget_show (send_button);
  1136.  
  1137.  
  1138.  
  1139. // Add messages text. Use columns 0 to 4 (exclusive) and rows 4 to 7 (exclusive)
  1140.  
  1141. list_messages = gtk_list_store_new (1, G_TYPE_STRING);
  1142.  
  1143. //update_list_rooms();
  1144.  
  1145. list = create_list ("Messages", list_messages);
  1146.  
  1147. gtk_table_attach_defaults (GTK_TABLE (table), list, 0, 6, 10, 16);
  1148.  
  1149. //selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view));
  1150.  
  1151. //g_signal_connect_swapped(send_button, "clicked",
  1152.  
  1153. // G_CALLBACK(get_messages), selection);
  1154.  
  1155. gtk_widget_show (list);
  1156.  
  1157. // Add messages text. Use columns 0 to 4 (exclusive) and rows 4 to 7 (exclusive)
  1158.  
  1159.  
  1160.  
  1161. //button
  1162.  
  1163. GtkWidget *adduser_button = gtk_button_new_with_label ("Create Account");
  1164.  
  1165. gtk_table_attach_defaults(GTK_TABLE (table), adduser_button, 0, 2, 3, 4);
  1166.  
  1167. gtk_widget_show (adduser_button);
  1168.  
  1169.  
  1170.  
  1171. GtkWidget *addroom_button = gtk_button_new_with_label ("Create Room");
  1172.  
  1173. gtk_table_attach_defaults(GTK_TABLE (table), addroom_button, 0, 2, 8, 9);
  1174.  
  1175. gtk_widget_set_size_request(addroom_button, 80, 35);
  1176.  
  1177. gtk_widget_show (addroom_button);
  1178.  
  1179.  
  1180.  
  1181. GtkWidget *login_button = gtk_button_new_with_label ("Login");
  1182.  
  1183. gtk_table_attach_defaults(GTK_TABLE (table), login_button, 0, 2, 4, 5);
  1184.  
  1185. gtk_widget_show (login_button);
  1186.  
  1187.  
  1188.  
  1189. GtkWidget *createroom_button = gtk_button_new_with_label ("Create Room");
  1190.  
  1191. gtk_table_attach_defaults(GTK_TABLE (table), createroom_button, 0, 2, 8, 9);
  1192.  
  1193. //gtk_widget_show (createroom_button);
  1194.  
  1195.  
  1196.  
  1197. //username
  1198.  
  1199. entryuser = gtk_entry_new ();
  1200.  
  1201. gtk_entry_set_max_length (GTK_ENTRY (entryuser), 50);
  1202.  
  1203. g_signal_connect_swapped (adduser_button, "clicked",
  1204.  
  1205. G_CALLBACK (enteruser_callback),
  1206.  
  1207. entryuser);
  1208.  
  1209. g_signal_connect_swapped (login_button, "clicked",
  1210.  
  1211. G_CALLBACK (enteruser_callback),
  1212.  
  1213. entryuser);
  1214.  
  1215. //gtk_entry_set_text (GTK_ENTRY (entryuser), "Username");
  1216.  
  1217. tmp_pos = GTK_ENTRY (entryuser)->text_length;
  1218.  
  1219. gtk_table_attach_defaults(GTK_TABLE (table), entryuser, 0, 2, 1, 2);
  1220.  
  1221. gtk_widget_set_size_request(entryuser, 80, 30);
  1222.  
  1223. gtk_widget_show (entryuser);
  1224.  
  1225.  
  1226.  
  1227. //frames
  1228.  
  1229. frameuser = gtk_frame_new("USER INFO");
  1230.  
  1231. //gtk_frame_set_label_widget(frame, entryuser);
  1232.  
  1233. gtk_table_attach_defaults(GTK_TABLE (table), frameuser, 0, 2, 0, 5);
  1234.  
  1235. gtk_widget_show(frameuser);
  1236.  
  1237.  
  1238.  
  1239. frameroom = gtk_frame_new("ROOM INFO");
  1240.  
  1241. //gtk_frame_set_label_widget(frame, entryuser);
  1242.  
  1243. gtk_table_attach_defaults(GTK_TABLE (table), frameroom, 0, 2, 6, 9);
  1244.  
  1245. gtk_widget_show(frameroom);
  1246.  
  1247.  
  1248.  
  1249. //password
  1250.  
  1251. entrypass = gtk_entry_new ();
  1252.  
  1253. gtk_entry_set_max_length (GTK_ENTRY (entrypass), 50);
  1254.  
  1255. g_signal_connect_swapped (adduser_button, "clicked",
  1256.  
  1257. G_CALLBACK (enterpass_callback),
  1258.  
  1259. entrypass);
  1260.  
  1261. g_signal_connect_swapped (login_button, "clicked",
  1262.  
  1263. G_CALLBACK (enterpass_callback),
  1264.  
  1265. entrypass);
  1266.  
  1267. //gtk_entry_set_text (GTK_ENTRY (entrypass), "password");
  1268.  
  1269. gtk_entry_set_visibility (GTK_ENTRY (entrypass), FALSE);
  1270.  
  1271. tmp_pos = GTK_ENTRY (entrypass)->text_length;
  1272.  
  1273. gtk_table_attach_defaults(GTK_TABLE (table), entrypass, 0, 2, 2, 3);
  1274.  
  1275. gtk_widget_set_size_request(entrypass, 80, 30);
  1276.  
  1277. gtk_widget_show (entrypass);
  1278.  
  1279.  
  1280.  
  1281.  
  1282.  
  1283. //room name
  1284.  
  1285. entryroom = gtk_entry_new ();
  1286.  
  1287. gtk_entry_set_max_length (GTK_ENTRY (entryroom), 50);
  1288.  
  1289. g_signal_connect_swapped (addroom_button, "clicked",
  1290.  
  1291. G_CALLBACK (enterroom_callback),
  1292.  
  1293. entryroom);
  1294.  
  1295. g_signal_connect_swapped (createroom_button, "clicked",
  1296.  
  1297. G_CALLBACK (enterroom_callback),
  1298.  
  1299. entryroom);
  1300.  
  1301. //gtk_entry_set_text (GTK_ENTRY (entryroom), "RoomName");
  1302.  
  1303. tmp_pos = GTK_ENTRY (entryroom)->text_length;
  1304.  
  1305. gtk_table_attach_defaults(GTK_TABLE (table), entryroom, 0, 2, 7, 8);
  1306.  
  1307. gtk_widget_set_size_request(entryroom, 80, 32);
  1308.  
  1309. gtk_widget_show (entryroom);
  1310.  
  1311.  
  1312.  
  1313. //Send Message
  1314.  
  1315.  
  1316.  
  1317. sendmessage = gtk_entry_new ();
  1318.  
  1319. gtk_entry_set_max_length (GTK_ENTRY (sendmessage), 300);
  1320.  
  1321. g_signal_connect_swapped (send_button, "clicked",
  1322.  
  1323. G_CALLBACK (message_callback),
  1324.  
  1325. sendmessage);
  1326.  
  1327. gtk_widget_set_size_request(sendmessage, 80, 50);
  1328.  
  1329. //gtk_entry_set_text (GTK_ENTRY (sendmessage), "Send message");
  1330.  
  1331. tmp_pos = GTK_ENTRY (sendmessage)->text_length;
  1332.  
  1333. gtk_table_attach_defaults(GTK_TABLE (table), sendmessage, 0, 6, 16, 18);
  1334.  
  1335. gtk_widget_show (sendmessage);
  1336.  
  1337.  
  1338.  
  1339. gtk_widget_show (table);
  1340.  
  1341. gtk_widget_show (window);
  1342.  
  1343.  
  1344.  
  1345. g_timeout_add(1000, (GSourceFunc) time_handler, (gpointer) window);
  1346.  
  1347. time_handler(window);
  1348.  
  1349.  
  1350.  
  1351. gtk_main ();
  1352.  
  1353.  
  1354.  
  1355. return 0;
  1356.  
  1357. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement