summaryrefslogtreecommitdiffstats
path: root/chrome/common/net/notifier/communicator/mailbox.cc
blob: 86104bf5761f5c4a5955803c86674bc1f7ea0f8a (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/common/net/notifier/communicator/mailbox.h"

#include <assert.h>
#include <stdlib.h>

#include <stack>
#include <vector>

#include "base/logging.h"
#include "chrome/common/net/notifier/base/string.h"
#include "chrome/common/net/notifier/base/utils.h"
#include "chrome/common/net/notifier/communicator/xml_parse_helpers.h"
#include "talk/base/basictypes.h"
#include "talk/base/common.h"
#include "talk/base/stringutils.h"
#include "talk/xmllite/xmlelement.h"
#include "talk/xmpp/constants.h"

namespace notifier {

// Labels are a list of strings seperated by a '|' character. The '|' character
// is escaped with a backslash ('\\') and the backslash is also escaped with a
// backslash.
static void ParseLabelSet(const std::string& text,
                          MessageThread::StringSet* labels) {
  const char* input_cur = text.c_str();
  const char* input_end = input_cur + text.size();
  char* result = new char[text.size() + 1];
  char* next_write = result;

  while (input_cur < input_end) {
    if (*input_cur == '|') {
      if (next_write != result) {
        *next_write = '\0';
        labels->insert(std::string(result));
        next_write = result;
      }
      input_cur++;
      continue;
    }

    if (*input_cur == '\\') {
      // Skip a character in the input and break if we are at the end.
      input_cur++;
      if (input_cur >= input_end)
        break;
    }
    *next_write = *input_cur;
    next_write++;
    input_cur++;
  }

  if (next_write != result) {
    *next_write = '\0';
    labels->insert(std::string(result));
  }

  delete [] result;
}

// -----------------------------------------------------------------------------

std::string MailAddress::safe_name() const {
  if (!name().empty()) {
    return name();
  }

  if (!address().empty()) {
    size_t at = address().find('@');
    if (at == std::string::npos) {
      return address();
    }

    if (at != 0) {
      return address().substr(0, at);
    }
  }

  return std::string("(unknown)");
}

// -----------------------------------------------------------------------------
MessageThread::~MessageThread() {
  Clear();
}

void MessageThread::Clear() {
  delete labels_;
  labels_ = NULL;

  delete senders_;
  senders_ = NULL;
}

MessageThread& MessageThread::operator=(const MessageThread& r) {
  if (&r != this) {
    Clear();
    // Copy everything.
    r.AssertValid();
    thread_id_ = r.thread_id_;
    date64_ = r.date64_;
    message_count_ = r.message_count_;
    personal_level_ = r.personal_level_;
    subject_ = r.subject_;
    snippet_ = r.snippet_;

    if (r.labels_)
      labels_ = new StringSet(*r.labels_);
    else
      labels_ = new StringSet;
    if (r.senders_)
      senders_ = new MailSenderList(*r.senders_);
    else
      senders_ = new MailSenderList;
  }
  AssertValid();
  return *this;
}

MessageThread* MessageThread::CreateFromXML(
    const buzz::XmlElement* src) {
  MessageThread* info = new MessageThread();
  if (!info || !info->InitFromXml(src)) {
    delete info;
    return NULL;
  }
  return info;
}

// Init from a chunk of XML.
bool MessageThread::InitFromXml(const buzz::XmlElement* src) {
  labels_ = new StringSet;
  senders_ = new MailSenderList;

  if (src->Name() != buzz::kQnMailThreadInfo)
    return false;

  if (!ParseInt64Attr(src, buzz::kQnMailTid, &thread_id_))
    return false;
  if (!ParseInt64Attr(src, buzz::kQnMailDate, &date64_))
    return false;
  if (!ParseIntAttr(src, buzz::kQnMailMessages, &message_count_))
    return false;
  if (!ParseIntAttr(src, buzz::kQnMailParticipation, &personal_level_))
    return false;

  const buzz::XmlElement* senders = src->FirstNamed(buzz::kQnMailSenders);
  if (!senders)
    return false;
  for (const buzz::XmlElement* child = senders->FirstElement();
       child != NULL;
       child = child->NextElement()) {
    if (child->Name() != buzz::kQnMailSender)
      continue;
    std::string address;
    if (!ParseStringAttr(child, buzz::kQnMailAddress, &address))
      continue;
    std::string name;
    ParseStringAttr(child, buzz::kQnMailName, &name);
    bool originator = false;
    ParseBoolAttr(child, buzz::kQnMailOriginator, &originator);
    bool unread = false;
    ParseBoolAttr(child, buzz::kQnMailUnread, &unread);

    senders_->push_back(MailSender(name, address, unread, originator));
  }

  const buzz::XmlElement* labels = src->FirstNamed(buzz::kQnMailLabels);
  if (!labels)
    return false;
  ParseLabelSet(labels->BodyText(), labels_);

  const buzz::XmlElement* subject = src->FirstNamed(buzz::kQnMailSubject);
  if (!subject)
    return false;
  subject_ = subject->BodyText();

  const buzz::XmlElement* snippet = src->FirstNamed(buzz::kQnMailSnippet);
  if (!snippet)
    return false;
  snippet_ = snippet->BodyText();

  AssertValid();
  return true;
}

bool MessageThread::starred() const {
  return (labels_->find("^t") != labels_->end());
}

bool MessageThread::unread() const {
  return (labels_->find("^u") != labels_->end());
}

#if defined(DEBUG)
// Non-debug version is inline and empty.
void MessageThread::AssertValid() const {
  assert(thread_id_ != 0);
  assert(senders_ != NULL);
  // In some (odd) cases, gmail may send email with no sender.
  // assert(!senders_->empty());
  assert(message_count_ > 0);
  assert(labels_ != NULL);
}
#endif

MailBox* MailBox::CreateFromXML(const buzz::XmlElement* src) {
  MailBox* mail_box = new MailBox();
  if (!mail_box || !mail_box->InitFromXml(src)) {
    delete mail_box;
    return NULL;
  }
  return mail_box;
}

// Init from a chunk of XML.
bool MailBox::InitFromXml(const buzz::XmlElement* src) {
  if (src->Name() != buzz::kQnMailBox)
    return false;

  if (!ParseIntAttr(src, buzz::kQnMailTotalMatched, &mailbox_size_))
    return false;

  estimate_ = false;
  ParseBoolAttr(src, buzz::kQnMailTotalEstimate, &estimate_);

  first_index_ = 0;
  ParseIntAttr(src, buzz::kQnMailFirstIndex, &first_index_);

  result_time_ = 0;
  ParseInt64Attr(src, buzz::kQnMailResultTime, &result_time_);

  highest_thread_id_ = 0;

  const buzz::XmlElement* thread_element =
      src->FirstNamed(buzz::kQnMailThreadInfo);
  while (thread_element) {
    MessageThread* thread = MessageThread::CreateFromXML(thread_element);
    if (thread) {
      if (thread->thread_id() > highest_thread_id_)
        highest_thread_id_ = thread->thread_id();
      threads_.push_back(MessageThreadPointer(thread));
    }
    thread_element = thread_element->NextNamed(buzz::kQnMailThreadInfo);
  }
  return true;
}

const size_t kMaxShortnameLength = 12;

// Tip: If you extend this list of chars, do not include '-'.
const char name_delim[] = " ,.:;\'\"()[]{}<>*@";

class SenderFormatter {
 public:
  // sender should not be deleted while this class is in use.
  SenderFormatter(const MailSender& sender,
                  const std::string& me_address)
      : sender_(sender),
        visible_(false),
        short_format_(true),
        space_(kMaxShortnameLength) {
    me_ = talk_base::ascicmp(me_address.c_str(),
                             sender.address().c_str()) == 0;
  }

  bool visible() const {
    return visible_;
  }

  bool is_unread() const {
    return sender_.unread();
  }

  const std::string name() const {
    return name_;
  }

  void set_short_format(bool short_format) {
    short_format_ = short_format;
    UpdateName();
  }

  void set_visible(bool visible) {
    visible_ = visible;
    UpdateName();
  }

  void set_space(size_t space) {
    space_ = space;
    UpdateName();
  }

 private:
  // Attempt to shorten to the first word in a person's name We could revisit
  // and do better at international punctuation, but this is what cricket did,
  // and it should be removed soon when gmail does the notification instead of
  // us forming it on the client.
  static void ShortenName(std::string* name) {
    size_t start = name->find_first_not_of(name_delim);
    if (start != std::string::npos && start > 0) {
      name->erase(0, start);
    }
    start = name->find_first_of(name_delim);
    if (start != std::string::npos) {
      name->erase(start);
    }
  }

  void UpdateName() {
    // Update the name if is going to be used.
    if (!visible_) {
      return;
    }

    if (me_) {
      name_ = "me";
      return;
    }

    if (sender_.name().empty() && sender_.address().empty()) {
      name_ = "";
      return;
    }

    name_ = sender_.name();
    // Handle the case of no name or a name looks like an email address. When
    // mail is sent to "Quality@example.com" <quality-team@example.com>, we
    // shouldn't show "Quality@example.com" as the name. Instead, use the email
    // address (without the @...)
    if (name_.empty() || name_.find_first_of("@") != std::string::npos) {
      name_ = sender_.address();
      size_t at_index = name_.find_first_of("@");
      if (at_index != std::string::npos) {
        name_.erase(at_index);
      }
    } else if (short_format_) {
      ShortenName(&name_);
    }

    if (name_.empty()) {
      name_ = "(unknown)";
    }

    // Abbreviate if too long.
    if (name_.size() > space_) {
      name_.replace(space_ - 1, std::string::npos, ".");
    }
  }

  const MailSender& sender_;
  std::string name_;
  bool visible_;
  bool short_format_;
  size_t space_;
  bool me_;
  DISALLOW_COPY_AND_ASSIGN(SenderFormatter);
};

const char kNormalSeparator[] = ",&nbsp;";
const char kEllidedSeparator[] = "&nbsp;..";

std::string FormatName(const std::string& name, bool bold) {
  std::string formatted_name;
  if (bold) {
    formatted_name.append("<b>");
  }
  formatted_name.append(HtmlEncode(name));
  if (bold) {
    formatted_name.append("</b>");
  }
  return formatted_name;
}

class SenderFormatterList {
 public:
  // sender_list must not be deleted while this class is being used.
  SenderFormatterList(const MailSenderList& sender_list,
                      const std::string& me_address)
      : state_(INITIAL_STATE),
        are_any_read_(false),
        index_(-1),
        first_unread_index_(-1) {
    // Add all read messages.
    const MailSender* originator = NULL;
    bool any_unread = false;
    for (size_t i = 0; i < sender_list.size(); ++i) {
      if (sender_list[i].originator()) {
        originator = &sender_list[i];
      }
      if (sender_list[i].unread()) {
        any_unread = true;
        continue;
      }
      are_any_read_ = true;
      if (!sender_list[i].originator()) {
        senders_.push_back(new SenderFormatter(sender_list[i],
                                               me_address));
      }
    }

    // There may not be an orignator (if there no senders).
    if (originator) {
      senders_.insert(senders_.begin(), new SenderFormatter(*originator,
                                                            me_address));
    }

    // Add all unread messages.
    if (any_unread) {
      // It should be rare, but there may be cases when all of the senders
      // appear to have read the message.
      first_unread_index_ = is_first_unread() ? 0 : senders_.size();
      for (size_t i = 0; i < sender_list.size(); ++i) {
        if (!sender_list[i].unread()) {
          continue;
        }
        // Don't add the originator if it is already at the start of the
        // "unread" list.
        if (sender_list[i].originator() && is_first_unread()) {
          continue;
        }
        senders_.push_back(new SenderFormatter(sender_list[i], me_address));
      }
    }
  }

  ~SenderFormatterList() {
    CleanupSequence(&senders_);
  }

  std::string GetHtml(int space) {
    index_ = -1;
    state_ = INITIAL_STATE;
    while (!added_.empty()) {
      added_.pop();
    }

    // If there is only one sender, let it take up all of the space.
    if (senders_.size() == 1) {
      senders_[0]->set_space(space);
      senders_[0]->set_short_format(false);
    }

    int length = 1;
    // Add as many senders as we can in the given space. Computes the visible
    // length at each iteration, but does not construct the actual html.
    while (length < space && AddNextSender()) {
      int new_length = ConstructHtml(is_first_unread(), NULL);
      // Remove names to avoid truncating
      // * if there will be at least 2 left or
      // * if the spacing <= 2 characters per sender and there
      //   is at least one left.
      if ((new_length > space &&
           (visible_count() > 2 ||
            (ComputeSpacePerSender(space) <= 2 && visible_count() > 1)))) {
        RemoveLastAddedSender();
        break;
      }
      length = new_length;
    }

    if (length > space) {
      int max = ComputeSpacePerSender(space);
      for (size_t i = 0; i < senders_.size(); ++i) {
        if (senders_[i]->visible()) {
          senders_[i]->set_space(max);
        }
      }
    }

    // Now construct the actual html.
    std::string html_list;
    length = ConstructHtml(is_first_unread(), &html_list);
    if (length > space) {
      LOG(LS_WARNING) << "LENGTH: " << length << " exceeds "
                      << space << " " << html_list;
    }
    return html_list;
  }

 private:
  int ComputeSpacePerSender(int space) const {
    // Why the "- 2"? To allow for the " .. " which may occur after the names,
    // and no matter what always allow at least 2 characters per sender.
    return talk_base::_max<int>(space / visible_count() - 2, 2);
  }

  // Finds the next sender that should be added to the "from" list and sets it
  // to visible.
  //
  // This method may be called until it returns false or until
  // RemoveLastAddedSender is called.
  bool AddNextSender() {
    // The progression is:
    //   1. Add the person who started the thread, which is the first message.
    //   2. Add the first unread message (unless it has already been added).
    //   3. Add the last message (unless it has already been added).
    //   4. Add the message that is just before the last message processed
    //      (unless it has already been added).
    //      If there is no message (i.e. at index -1), return false.
    //
    // Typically, this method is called until it returns false or all of the
    // space available is used.
    switch (state_) {
      case INITIAL_STATE:
        state_ = FIRST_MESSAGE;
        index_ = 0;
        // If the server behaves odd and doesn't send us any senders, do
        // something graceful.
        if (senders_.size() == 0) {
          return false;
        }
        break;

      case FIRST_MESSAGE:
        if (first_unread_index_ >= 0) {
          state_ = FIRST_UNREAD_MESSAGE;
          index_ = first_unread_index_;
          break;
        }
        // Fall through.
      case FIRST_UNREAD_MESSAGE:
        state_ = LAST_MESSAGE;
        index_ = senders_.size() - 1;
        break;

      case LAST_MESSAGE:
      case PREVIOUS_MESSAGE:
        state_ = PREVIOUS_MESSAGE;
        index_--;
        break;

      case REMOVED_MESSAGE:
      default:
        LOG(DFATAL) << "invalid state: " << state_;
        return false;
    }

    if (index_ < 0) {
      return false;
    }

    if (!senders_[index_]->visible()) {
      added_.push(index_);
      senders_[index_]->set_visible(true);
    }
    return true;
  }

  // Makes the last added sender not visible.
  //
  // May be called while visible_count() > 0.
  void RemoveLastAddedSender() {
    state_ = REMOVED_MESSAGE;
    int index = added_.top();
    added_.pop();
    senders_[index]->set_visible(false);
  }

  // Constructs the html of the SenderList and returns the length of the
  // visible text.
  //
  // The algorithm simply walks down the list of Senders, appending the html
  // for each visible sender, and adding ellipsis or commas in between,
  // whichever is appropriate.
  //
  // html Filled with html.  Maybe NULL if the html doesn't need to be
  //      constructed yet (useful for simply determining the length of the
  //      visible text).
  //
  // returns The approximate visible length of the html.
  int ConstructHtml(bool first_is_unread,
                    std::string* html) const {
    if (senders_.empty()) {
      return 0;
    }

    int length = 0;

    // The first is always visible.
    const SenderFormatter* sender = senders_[0];
    const std::string& originator_name = sender->name();
    length += originator_name.length();
    if (html) {
      html->append(FormatName(originator_name, first_is_unread));
    }

    bool elided = false;
    const char* between = "";
    for (size_t i = 1; i < senders_.size(); i++) {
      sender = senders_[i];

      if (sender->visible()) {
        // Handle the separator.
        between = elided ? "&nbsp;" : kNormalSeparator;
        // Ignore the , for length because it is so narrow, so in both cases
        // above the space is the only things that counts for spaces.
        length++;

        // Handle the name.
        const std::string name = sender->name();
        length += name.size();

        // Construct the html.
        if (html) {
          html->append(between);
          html->append(FormatName(name, sender->is_unread()));
        }
        elided = false;
      } else if (!elided) {
        between = kEllidedSeparator;
        length += 2;  // ".." is narrow.
        if (html) {
          html->append(between);
        }
        elided = true;
      }
    }
    return length;
  }

  bool is_first_unread() const {
    return !are_any_read_;
  }

  size_t visible_count() const {
    return added_.size();
  }

  enum MessageState {
    INITIAL_STATE,
    FIRST_MESSAGE,
    FIRST_UNREAD_MESSAGE,
    LAST_MESSAGE,
    PREVIOUS_MESSAGE,
    REMOVED_MESSAGE,
  };

  // What state we were in during the last "stateful" function call.
  MessageState state_;
  bool are_any_read_;
  std::vector<SenderFormatter*> senders_;
  std::stack<int> added_;
  int index_;
  int first_unread_index_;
  DISALLOW_COPY_AND_ASSIGN(SenderFormatterList);
};


std::string GetSenderHtml(const MailSenderList& sender_list,
                          int message_count,
                          const std::string& me_address,
                          int space) {
  // There has to be at least 9 spaces to show something reasonable.
  DCHECK_GE(space, 10);
  std::string count_html;
  if (message_count > 1) {
    std::string count(IntToString(message_count));
    space -= (count.size());
    count_html.append("&nbsp;(");
    count_html.append(count);
    count_html.append(")");
    // Reduce space due to parenthesis and &nbsp;.
    space -= 2;
  }

  SenderFormatterList senders(sender_list, me_address);
  std::string html_list(senders.GetHtml(space));
  html_list.append(count_html);
  return html_list;
}

}  // namespace notifier