aboutsummaryrefslogtreecommitdiff
path: root/contactWind.c
blob: f57465c2771a0b25871d6940275a60787764ba17 (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#include "contactWind.h"

extern contact* contactList;
extern size_t nbContacts;


int x_menuContact = 1,y_menuContact = 1;
int w_menuContact,h_menuContact;

int contactListSelection;
int firstConctactShow;

int showName = 1;


contact* getContactSelected(void) {
    return contactListSelection+contactList;
}
void selectNextContact(void) {
    if( contactListSelection < nbContacts - 1)
        contactListSelection++;
}
void setContactSelection(contact* c) {
    int i = c - contactList ;
    int h = h_menuContact;

    if( i >= 0 && i < nbContacts )
        contactListSelection = i;

    while( contactListSelection < firstConctactShow ) {
        firstConctactShow-=h/2;
        if( firstConctactShow < 0 )
            firstConctactShow = 0;
    }
    while( contactListSelection >= firstConctactShow + h ) {
        firstConctactShow+=h/2;
        if( firstConctactShow > nbContacts-1 )
            firstConctactShow = nbContacts-1;
    }
}
void selectPreviousContact(void) {
    if( contactListSelection > 0)
        contactListSelection--;
}
void clearContactListW(void) {
    for(int i = firstConctactShow; i < h_menuContact && i < nbContacts; i++) {
        move(y_menuContact+i,x_menuContact);
        printw("%*s", w_menuContact, " ");
    }
}

void showLineContactW(contact* c, int x, int y, int i, char* tmp) {
    int nbMessage = 0, isLastToRespond = 0, hasNewMessage = 0;
    char dateLastSMS[5] = "";
    time_t now_s = time(NULL);
    struct tm *now = localtime(&now_s);

    int color = 0, attr = A_NORMAL;

    if( c->conversation ) {
        attr |= A_BOLD;

        sms* lastSMS = &c->conversation->listSMS[c->conversation->nbSMS-1];
        struct tm* dateOut = localtime(&lastSMS->outDate);

        hasNewMessage = lastSMS->state == SMS_UNREAD;
        isLastToRespond = !lastSMS->in_out;
        nbMessage = c->conversation->nbSMS;

        if( dateOut->tm_year == now->tm_year ) {
            sprintf(dateLastSMS, "%02d/%02d", dateOut->tm_mday, dateOut->tm_mon + 1);
        } else {
            sprintf(dateLastSMS, " %04d", dateOut->tm_year);
        }
    }
    if( i == contactListSelection ) {
        attr |= A_REVERSE;
    }

    attrset( COLOR_PAIR(color) | attr);
    move(y,0);
    printw("%*s", w_menuContact+1, "");

    // First of all isLastToRespond !
    if( isLastToRespond && *c->number == '+' && *c->display_name != '+') {
        attrset( COLOR_PAIR( 11 ) | attr );
        mvprintc(0,y," ", 1);
        attrset( COLOR_PAIR(color) | attr);
    }

    // Then contact name
    if( showName )
        mvprintc(x,y, contactList[i].display_name, w_menuContact-10);
    else
        mvprintc(x,y,contactList[i].number, w_menuContact-10);

    // Date
    if( *dateLastSMS ) {
        attrset( COLOR_PAIR(5) | attr );
        mvprintc(x + w_menuContact - 5,y, dateLastSMS, 5);
        attrset( COLOR_PAIR(color) | attr);
    }

    // Then nb message
    int r = 0;
    if( nbMessage > 0 ) {
        attrset( COLOR_PAIR(6) | attr);
        r = sprintf(tmp, "%d", nbMessage);
        mvprintc(x + w_menuContact - 6 - r,y, tmp, 5);
        attrset( COLOR_PAIR(color) | attr);
    }

    // Then new message notification Ne
    if( hasNewMessage ) {
        move(y,x + w_menuContact - 8 - r );
        printw("✉️");
    }
    y++;
    attrset(0 | A_NORMAL);
}

void showContactListW(void) {

    int x = x_menuContact;
    int y = y_menuContact;
    int w = w_menuContact;
    int h = h_menuContact;
    int s = nbContacts;

    if( contactListSelection < firstConctactShow ) {
        firstConctactShow-=h/2;
        if( firstConctactShow < 0 )
            firstConctactShow = 0;
    }

    if( contactListSelection >= firstConctactShow + h ) {
        attrset(0 | A_NORMAL );
        firstConctactShow+=h/2;
        if( firstConctactShow > nbContacts-1 )
            firstConctactShow = nbContacts-1;
    }

    char* line = malloc( w+1 );

    int i;
    for(i = firstConctactShow ;  i < (h+firstConctactShow) && i < s ; i++) {
        showLineContactW(contactList+i, x, y+i-firstConctactShow, i, line);
    }

    for( ; i < (h+firstConctactShow) ; i++){ // Clear
        move(y+i-firstConctactShow,0);
        printw("%*s", w+1, "");
    }

    free(line);
}