drawing: improve default avatar generation

The default avatar is the first letter of the bestName of the
ContactMethod behind a circle with a random color from a palette.

Change-Id: Idde0a4830592d8286dcc34d2f3d5eafd9ae9064d
Reviewed-by: Philippe Gorley <philippe.gorley@savoirfairelinux.com>
diff --git a/src/native/pixbufmanipulator.cpp b/src/native/pixbufmanipulator.cpp
index 032cb00..1d9553b 100644
--- a/src/native/pixbufmanipulator.cpp
+++ b/src/native/pixbufmanipulator.cpp
@@ -30,12 +30,38 @@
 namespace Interfaces {
 
 PixbufManipulator::PixbufManipulator()
-    : fallbackAvatar_{ring_draw_fallback_avatar(FALLBACK_AVATAR_SIZE), g_object_unref}
-    , conferenceAvatar_{ring_draw_conference_avatar(FALLBACK_AVATAR_SIZE), g_object_unref}
+    : conferenceAvatar_{ring_draw_conference_avatar(FALLBACK_AVATAR_SIZE), g_object_unref}
 {
 }
 
 std::shared_ptr<GdkPixbuf>
+PixbufManipulator::generateAvatar(const ContactMethod* cm) const
+{
+    auto cm_number = QString("0");
+    auto letter = QChar('R'); // R for ring
+    if (cm) {
+        auto hashName = cm->uri().userinfo();
+        if (hashName.size() > 0) {
+            cm_number = hashName.at(0);
+        }
+        letter = cm->bestName().toUpper().at(0);
+    }
+
+    bool ok;
+    auto color = cm_number.toUInt(&ok, 16);
+    if (!ok) color = 0;
+
+    return std::shared_ptr<GdkPixbuf> {
+        ring_draw_fallback_avatar(
+            FALLBACK_AVATAR_SIZE,
+            letter.toLatin1(),
+            color
+        ),
+        g_object_unref
+    };
+}
+
+std::shared_ptr<GdkPixbuf>
 PixbufManipulator::scaleAndFrame(const GdkPixbuf *photo, const QSize& size, bool display_presence, bool is_present)
 {
     /**
@@ -91,7 +117,7 @@
     if (n->contact()) {
         return contactPhoto(n->contact(), size, displayPresence);
     } else {
-        return QVariant::fromValue(scaleAndFrame(fallbackAvatar_.get(), size, displayPresence, n->isPresent()));
+        return QVariant::fromValue(scaleAndFrame(generateAvatar(n).get(), size, displayPresence, n->isPresent()));
     }
 }
 
@@ -100,15 +126,17 @@
 {
     /**
      * try to get the photo
-     * otherwise use the fallback avatar
+     * otherwise use the generated avatar
      */
 
     std::shared_ptr<GdkPixbuf> photo;
 
     if (c->photo().isValid())
         photo = c->photo().value<std::shared_ptr<GdkPixbuf>>();
-    else
-        photo = fallbackAvatar_;
+    else {
+        auto cm = c->phoneNumbers().size() > 0 ? c->phoneNumbers().first() : nullptr;
+        photo = generateAvatar(cm);
+    }
 
     return QVariant::fromValue(scaleAndFrame(photo.get(), size, displayPresence, c->isPresent()));
 }