File: statistic.cpp

package info (click to toggle)
gcin 2.7.6.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 17,408 kB
  • sloc: cpp: 28,181; ansic: 9,208; makefile: 576; sh: 504
file content (131 lines) | stat: -rw-r--r-- 2,457 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "gcin.h"

#define MAX_KEPT_TIME (5 * 60 * (gint64)1000000)
static gint64 *ch_time;
static int ch_timeN, ch_timeN_a;
static GtkWidget *label_disp, *gwin_stat;
gboolean stat_enabled;
static int timeout_handle;

static int get_ch_count(int mini)
{
  gint64 t = current_time() -  mini * 60 * 1000000;

  int i, N;
  for(i=0,N=0; i < ch_timeN; i++)
    if (ch_time[i] >= t)
      N++;

 return ((double)N/mini + 0.5);
}


void disp_stat()
{
  char tt[512];

  sprintf(tt, _(_L("1,3,5分鐘\n%d,%d,%d/分")), get_ch_count(1), get_ch_count(3), get_ch_count(5));
  gtk_label_set_text(GTK_LABEL(label_disp), tt);
#if WIN32
  gtk_window_present(GTK_WINDOW(gwin_stat));
#endif
}

void add_ch_time()
{
  int i;
  gint64 tim = current_time();
  gint64 tim_exp = tim - MAX_KEPT_TIME;

  for(i=0;i<ch_timeN;i++)
    if (ch_time[i] > tim_exp)
      break;

  if (i) {
    int leftN = ch_timeN - i;
    memmove(ch_time, ch_time+i, sizeof(gint64) * leftN);
    ch_timeN = leftN;
  }

  if (ch_timeN_a <= ch_timeN+1) {
    ch_timeN_a =ch_timeN+1;
    ch_time = trealloc(ch_time, gint64, ch_timeN_a);
  }

  ch_time[ch_timeN++]=tim;
}

void add_ch_time_str(char *s)
{
  int len= strlen(s);
  int i=0;
  while (i< len) {
    if (!(s[i] & 0x80)) {
      i++;
      continue;
    }
    i+=utf8_sz(s+i);
    add_ch_time();
  }

  if (stat_enabled)
    disp_stat();
}

void destory_win_stat()
{
  if (!gwin_stat)
    return;
  gtk_widget_destroy(gwin_stat);
  gwin_stat = NULL;
  g_source_remove(timeout_handle);
}


gboolean timeout_update_stat(gpointer data)
{
  disp_stat();
  return TRUE;
}


void create_stat_win()
{
  gwin_stat = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_has_resize_grip(GTK_WINDOW(gwin_stat), FALSE);
#if WIN32
  set_no_focus(gwin_stat);
#endif
  gtk_container_set_border_width (GTK_CONTAINER (gwin_stat), 0);
  gtk_widget_realize (gwin_stat);
#if UNIX
  GdkWindow *gdkwin0 = gtk_widget_get_window(gwin_stat);
  set_no_focus(gwin_stat);
#else
  win32_init_win(gwin_stat);
#endif


  GtkWidget *vbox = gtk_vbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER (gwin_stat), vbox);


  label_disp = gtk_label_new(NULL);

  gtk_box_pack_start (GTK_BOX (vbox), label_disp, TRUE, TRUE, 0);

  gtk_widget_show_all(gwin_stat);
  timeout_handle = g_timeout_add(3000, timeout_update_stat, NULL);
}



void toggle_stat_win()
{
  stat_enabled ^= 1;
  if (stat_enabled) {
    create_stat_win();
    disp_stat();
  } else
    destory_win_stat();
}