@Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.info:
        View infoView =
            getActivity().getLayoutInflater().inflate(R.layout.dialog_channel_info, null);
        TextView topicView = (TextView) infoView.findViewById(R.id.topic);
        topicView.setText(mChannel.getTopic());
        Linkify.addLinks(topicView, Linkify.WEB_URLS);
        // FIXME: modes, etc...
        new AlertDialog.Builder(getActivity())
            .setTitle(getString(R.string.info_format, mChannel.getName()))
            .setView(infoView)
            .setPositiveButton(android.R.string.ok, null)
            .show();
        return true;

      case R.id.members:
        Intent intent = new Intent(getActivity(), MemberListActivity.class);
        intent.putExtra(ARG_CONNECTION_ID, getArguments().getLong(ARG_CONNECTION_ID));
        intent.putExtra(ARG_BUFFER_ID, getArguments().getLong(ARG_BUFFER_ID));
        startActivity(intent);
        return true;

      case R.id.part_channel:
        mChannel.part();
        return true;
    }
    return super.onOptionsItemSelected(item);
  }
  @Override
  public void onPrepareOptionsMenu(android.view.Menu menu) {
    super.onPrepareOptionsMenu(menu);
    boolean isJoined = (mChannel != null) && mChannel.isJoined();
    menu.findItem(R.id.info).setVisible(isJoined);
    menu.findItem(R.id.members).setVisible(isJoined);
    menu.findItem(R.id.part_channel).setVisible(isJoined);

    // User must part channel before archiving/deleting
    for (int id : new int[] {R.id.archive, R.id.delete}) {
      MenuItem item = menu.findItem(id);
      item.setVisible(item.isVisible() && (!isJoined));
    }
  }
  @Override
  protected void updateUI() {
    super.updateUI();
    if (getView() == null) {
      // View not yet created.
      return;
    }

    boolean isConnected =
        (mConnection != null && mConnection.getState() == Connection.STATE_CONNECTED);
    boolean isJoined = (mChannel != null && mChannel.isJoined());

    View notInChannelView = getView().findViewById(R.id.not_in_channel);
    notInChannelView.setVisibility((!isConnected) || isJoined ? View.GONE : View.VISIBLE);

    getActivity().invalidateOptionsMenu();
  }